src/EventSubscriber/ClientAccountCloseSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Domain\Exception\Client\ClientNotFoundException;
  4. use App\Domain\Middleware\AccountRestrictInterface;
  5. use App\Services\Clients\ClientSystemState\ClientSystemStateService;
  6. use App\Services\Auth\TokenClientUserService;
  7. use App\Entity\Client;
  8. use App\Repository\ClientRepository;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  11. class ClientAccountCloseSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var TokenClientUserService
  15.      */
  16.     private $tokenService;
  17.     /**
  18.      * @var ClientSystemStateService
  19.      */
  20.     private $systemStateService;
  21.     /**
  22.      * @var ClientRepository
  23.      */
  24.     private $clientRepository;
  25.     public function __construct(
  26.         TokenClientUserService $tokenService,
  27.         ClientSystemStateService $systemStateService,
  28.         ClientRepository $clientRepository
  29.     ) {
  30.         $this->tokenService $tokenService;
  31.         $this->systemStateService $systemStateService;
  32.         $this->clientRepository $clientRepository;
  33.     }
  34.     public function onKernelController(FilterControllerEvent $event)
  35.     {
  36.         $controller $event->getController();
  37.         /*
  38.          * $controller passed can be either a class or a Closure.
  39.          * This is not usual in Symfony but it may happen.
  40.          * If it is a class, it comes in array format
  41.          */
  42.         if (!is_array($controller)) {
  43.             return;
  44.         }
  45.         $route $event->getRequest()->attributes->get('_route');
  46.         if ($controller[0] instanceof AccountRestrictInterface && $route == 'clientCloseAccount') {
  47.             /** @var Client $currentClient */
  48.             $currentClient $this->tokenService->getCurrentClient();
  49.             $this->systemStateService->allowUseSystem($currentClient);
  50.             $owner $event->getRequest()->get('owner');
  51.             if ($owner && isset($owner['client_id'])) {
  52.                 /** @var Client $ownerClient */
  53.                 $ownerClient $this->clientRepository->find($owner['client_id']);
  54.                 if (!$ownerClient) {
  55.                     throw new ClientNotFoundException();
  56.                 }
  57.                 $this->systemStateService->allowUseSystem($ownerClient);
  58.             }
  59.         }
  60.     }
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             'kernel.controller' => 'onKernelController',
  65.         ];
  66.     }
  67. }