src/EventListener/VentourRequestChecker.php line 103

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Controller\AppController;
  4. use App\Controller\VentourController;
  5. use App\Controller\VentourVentourController;
  6. use App\Entity\License;
  7. use App\Exception\VentourApiControllerException;
  8. use App\Helper\LicenseHelper;
  9. use App\Helper\MuseumHelper;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Exception;
  12. use ReflectionClass;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  21. use Symfony\Contracts\EventDispatcher\Event;
  22. /**
  23.  * Class VentourRequestChecker
  24.  * @package App\EventListener
  25.  */
  26. class VentourRequestChecker
  27. {
  28.     /**
  29.      * @var AuthorizationCheckerInterface
  30.      */
  31.     private $authorizationChecker;
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     private $entityManager;
  36.     /**
  37.      * @var LicenseHelper
  38.      */
  39.     private $licenseHelper;
  40.     /**
  41.      * @var MuseumHelper
  42.      */
  43.     private $museumHelper;
  44.     /**
  45.      * @var RouterInterface
  46.      */
  47.     private $router;
  48.     /**
  49.      * @var TokenStorageInterface
  50.      */
  51.     private $tokenStorage;
  52.     /**
  53.      * VentourRequestChecker constructor.
  54.      * @param LicenseHelper                 $licenseHelper
  55.      * @param MuseumHelper                  $museumHelper
  56.      * @param EntityManagerInterface        $entityManager
  57.      * @param RouterInterface               $router
  58.      * @param TokenStorageInterface         $tokenStorage
  59.      * @param AuthorizationCheckerInterface $authorizationChecker
  60.      */
  61.     public function __construct(
  62.         LicenseHelper $licenseHelper,
  63.         MuseumHelper $museumHelper,
  64.         EntityManagerInterface $entityManager,
  65.         RouterInterface $router,
  66.         TokenStorageInterface $tokenStorage,
  67.         AuthorizationCheckerInterface $authorizationChecker
  68.     ) {
  69.         $this->licenseHelper        $licenseHelper;
  70.         $this->entityManager        $entityManager;
  71.         $this->router               $router;
  72.         $this->museumHelper         $museumHelper;
  73.         $this->tokenStorage         $tokenStorage;
  74.         $this->authorizationChecker $authorizationChecker;
  75.     }
  76.     /**
  77.      * @param ExceptionEvent $event
  78.      */
  79.     public function onKernelException(ExceptionEvent $event)
  80.     {
  81.         $exception $event->getThrowable();
  82.         if ($exception instanceof VentourApiControllerException) {
  83.             $errorCode 400;
  84.             if ($exception->getCode() === VentourApiControllerException::MUSEUM_NOT_FOUND) {
  85.                 $errorCode 404;
  86.             }
  87.             $event->setResponse(new JsonResponse($exception->getErrorData(), $errorCode));
  88.         }
  89.     }
  90.     /**
  91.      * @param RequestEvent $event
  92.      * @throws \ReflectionException
  93.      * @throws Exception
  94.      */
  95.     public function onKernelRequest(RequestEvent $event)
  96.     {
  97.         if ($event->isMasterRequest()) {
  98.             $license $this->entityManager->getRepository(License::class)
  99.                                            ->findByDomain($event->getRequest()->getHttpHost());
  100.             // Get controller class
  101.             /**
  102.              * @var string $controllerName
  103.              */
  104.             $controllerName explode('::'$event->getRequest()->attributes->get('_controller'))[0];
  105.             if (class_exists($controllerName)) {
  106.                 // If a valid class (not the profiler, for example)
  107.                 $controller = new ReflectionClass($controllerName);
  108.                 // If a VentourController, validate data, else, redirect to the correct controller
  109.                 if ($controller->isSubclassOf(new ReflectionClass(VentourController::class))) {
  110.                     $this->checkLicense($event$license$controllerName$controller);
  111.                     $this->licenseHelper->init($license);
  112.                 } elseif ($controllerName === AppController::class) {
  113.                     if ($license->isVentourType()) {
  114.                         $event->setResponse(new RedirectResponse($this->router->generate('select_museum')));
  115.                     } elseif ($license->isAdminType()) {
  116.                         $event->setResponse(new RedirectResponse($this->router->generate('home_admin')));
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122.     /**
  123.      * @param RequestEvent    $event
  124.      * @param License         $license
  125.      * @param string          $controllerName
  126.      * @param ReflectionClass $controller
  127.      * @throws Exception
  128.      */
  129.     private function checkLicense(
  130.         RequestEvent $event,
  131.         License $license,
  132.         string $controllerName,
  133.         ReflectionClass $controller
  134.     ): void {
  135.         /**
  136.          * @var VentourController $controllerName
  137.          */
  138.         if ($license->getType()->getType() !== $controllerName::getRequiredLicenseType()) {
  139.             $event->setResponse(new Response(''404));
  140.         } elseif ($controller->isSubclassOf(new ReflectionClass(VentourVentourController::class))) {
  141.             if (!$license->getType()->isVentourType()) {
  142.                 throw new Exception("'{$controllerName}' loaded in a '{$license->getType()}' license");
  143.             }
  144.             $this->checkMuseum($event);
  145.         }
  146.     }
  147.     /**
  148.      * @param RequestEvent $event
  149.      */
  150.     private function checkMuseum(RequestEvent $event): void
  151.     {
  152.         if ($this->isUserLoggedIn()
  153.             && $event->getRequest()->attributes->get('_route') !== 'select_museum'
  154.             && $event->getRequest()->attributes->get('_route') !== 'selected_museum'
  155.             && empty($this->museumHelper->getMuseum())) {
  156.             $url $this->router->generate('select_museum', array(), RouterInterface::ABSOLUTE_URL);
  157.             $event->setResponse(new RedirectResponse($url));
  158.         }
  159.     }
  160.     /**
  161.      * @return bool
  162.      */
  163.     private function isUserLoggedIn(): bool
  164.     {
  165.         return !empty($this->tokenStorage->getToken())
  166.                && !empty($this->authorizationChecker->isGranted('ROLE_USER'));
  167.     }
  168. }