src/EventSubscriber/SepaRestrictSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Domain\Exception\Restrict\RestrictException;
  4. use App\Domain\Middleware\PaymentRestrictInterface;
  5. use App\Services\Clients\ClientSystemState\ClientSystemStateService;
  6. use App\Services\Auth\TokenClientUserService;
  7. use App\Entity\Client;
  8. use App\Entity\ClientAccount;
  9. use App\Utils\TranslatorHelper;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  13. class SepaRestrictSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var TokenClientUserService
  17.      */
  18.     private $tokenService;
  19.     /**
  20.      * @var ClientSystemStateService
  21.      */
  22.     private $systemStateService;
  23.     /**
  24.      * @var EntityManagerInterface
  25.      */
  26.     private $entityManager;
  27.     public function __construct(
  28.         TokenClientUserService $tokenService,
  29.         ClientSystemStateService $systemStateService,
  30.         EntityManagerInterface $entityManager
  31.     ) {
  32.         $this->tokenService $tokenService;
  33.         $this->systemStateService $systemStateService;
  34.         $this->entityManager $entityManager;
  35.     }
  36.     public function onKernelController(FilterControllerEvent $event)
  37.     {
  38.         $controller $event->getController();
  39.         /*
  40.          * $controller passed can be either a class or a Closure.
  41.          * This is not usual in Symfony but it may happen.
  42.          * If it is a class, it comes in array format
  43.          */
  44.         if (!is_array($controller)) {
  45.             return;
  46.         }
  47.         $route $event->getRequest()->attributes->get('_route');
  48.         if ($controller[0] instanceof PaymentRestrictInterface && $route == 'clientCreatePayment') {
  49.             /** @var Client $currentClient */
  50.             $currentClient $this->tokenService->getCurrentClient();
  51.             $this->systemStateService->allowUseSystem($currentClient);
  52.             $accountId $event->getRequest()->get('dr_acc_id');
  53.             /** @var ClientAccount $clientAccount* */
  54.             $clientAccount $this->entityManager->getRepository(ClientAccount::class)->findOneBy([
  55.                 'account' => $accountId,
  56.                 'client' => $currentClient->getId(),
  57.             ]);
  58.             if (!$clientAccount) {
  59.                 throw new RestrictException(json_encode([
  60.                     'message' => TranslatorHelper::trans('You are not allowed to make payments with this account id'),
  61.                 ]));
  62.             }
  63.             $this->systemStateService->allowUseSystem($clientAccount->getAccount()->getClient());
  64.         }
  65.     }
  66.     public static function getSubscribedEvents()
  67.     {
  68.         return [
  69.             'kernel.controller' => 'onKernelController',
  70.         ];
  71.     }
  72. }