src/EventSubscriber/ProspectControllerSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\V1\Prospect\AnonymousController;
  4. use App\Controller\V1\Prospect\ProspectAuthenticatedControllerInterface;
  5. use App\Controller\V1\Prospect\RegistrationController;
  6. use App\Repository\Account\TenantPersonRepository;
  7. use App\Service\TenantPerson\TenantPersonService;
  8. use Doctrine\ORM\NonUniqueResultException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class ProspectControllerSubscriber implements EventSubscriberInterface
  14. {
  15.     private $tenantPersonRepository;
  16.     private $tenantPersonService;
  17.     public function __construct(
  18.         TenantPersonRepository $tenantPersonRepository,
  19.         TenantPersonService $tenantPersonService
  20.     ) {
  21.         $this->tenantPersonRepository $tenantPersonRepository;
  22.         $this->tenantPersonService $tenantPersonService;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::CONTROLLER => 'onKernelController',
  28.         ];
  29.     }
  30.     /**
  31.      * @throws NonUniqueResultException
  32.      * @throws \Exception
  33.      */
  34.     public function onKernelController(ControllerEvent $event)
  35.     {
  36.         $controller $event->getController();
  37.         $request $event->getRequest();
  38.         // when a controller class defines multiple action methods, the controller
  39.         // is returned as [$controllerInstance, 'methodName']
  40.         if (is_array($controller)) {
  41.             $controller $controller[0];
  42.         }
  43.         if ($controller instanceof RegistrationController || $controller instanceof AnonymousController) {
  44.             return;
  45.         }
  46.         if ($controller instanceof ProspectAuthenticatedControllerInterface) {
  47.             $forbidden = new AccessDeniedHttpException("You don't have permission to access the server.");
  48.             if (!$request->headers->has('X-TP-ID')) {
  49.                 throw $forbidden;
  50.             }
  51.             $prospectTenantPersonId $request->headers->get('X-TP-ID');
  52.             if (!is_numeric($prospectTenantPersonId)) {
  53.                 throw $forbidden;
  54.             }
  55.             $tenantPerson $this->tenantPersonRepository->findOneByProspectTenantPersonId($prospectTenantPersonId);
  56.             if (!$tenantPerson) {
  57.                 throw $forbidden;
  58.             }
  59.             if ($this->tenantPersonService->isInitialized()) {
  60.                 return;
  61.             }
  62.             $this->tenantPersonService->init($tenantPerson);
  63.         }
  64.     }
  65. }