<?php
namespace App\EventSubscriber;
use App\Domain\Exception\Restrict\RestrictException;
use App\Domain\Middleware\PaymentRestrictInterface;
use App\Services\Clients\ClientSystemState\ClientSystemStateService;
use App\Services\Auth\TokenClientUserService;
use App\Entity\Client;
use App\Entity\ClientAccount;
use App\Utils\TranslatorHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class SepaRestrictSubscriber implements EventSubscriberInterface
{
/**
* @var TokenClientUserService
*/
private $tokenService;
/**
* @var ClientSystemStateService
*/
private $systemStateService;
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function __construct(
TokenClientUserService $tokenService,
ClientSystemStateService $systemStateService,
EntityManagerInterface $entityManager
) {
$this->tokenService = $tokenService;
$this->systemStateService = $systemStateService;
$this->entityManager = $entityManager;
}
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
/*
* $controller passed can be either a class or a Closure.
* This is not usual in Symfony but it may happen.
* If it is a class, it comes in array format
*/
if (!is_array($controller)) {
return;
}
$route = $event->getRequest()->attributes->get('_route');
if ($controller[0] instanceof PaymentRestrictInterface && $route == 'clientCreatePayment') {
/** @var Client $currentClient */
$currentClient = $this->tokenService->getCurrentClient();
$this->systemStateService->allowUseSystem($currentClient);
$accountId = $event->getRequest()->get('dr_acc_id');
/** @var ClientAccount $clientAccount* */
$clientAccount = $this->entityManager->getRepository(ClientAccount::class)->findOneBy([
'account' => $accountId,
'client' => $currentClient->getId(),
]);
if (!$clientAccount) {
throw new RestrictException(json_encode([
'message' => TranslatorHelper::trans('You are not allowed to make payments with this account id'),
]));
}
$this->systemStateService->allowUseSystem($clientAccount->getAccount()->getClient());
}
}
public static function getSubscribedEvents()
{
return [
'kernel.controller' => 'onKernelController',
];
}
}