src/Listener/OrlListener.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Service\OrlService;
  4. use Psr\Log\LoggerInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. class OrlListener
  8. {
  9.     private $logger;
  10.     private $orlService;
  11.     // route name only not path
  12.     private $whiteListOrRouteName = [
  13.         'orl_homepage',
  14.         'orl_finder',
  15.         'add_prospect_on_orl_office',
  16.         'orl_contact',
  17.         'orl_contact_send',
  18.         'orl_legal_notice',
  19.         'orl_privacy',
  20.         'orl_cgu'
  21.     ];
  22.     public function __construct(LoggerInterface $loggerOrlService $orlService)
  23.     {
  24.         $this->logger $logger;
  25.         $this->orlService $orlService;
  26.     }
  27.     public function __invoke(RequestEvent $event): void
  28.     {
  29.         $request $event->getRequest();
  30.         // ignore FOS JS routing init query
  31.         if($request->getRequestUri() === '/js/routing?callback=fos.Router.setData') {
  32.             return;
  33.         }
  34.         // Don't process events with HTTP exceptions - those have either been thrown
  35.         // by us or have nothing to do with this module.
  36.         if ($event->getRequest()->get('exception') != NULL) {
  37.             return;
  38.         }
  39.         // are we in a orl app
  40.         if($this->orlService->isOrlWebsite()){
  41.             //check route name
  42.             $request $event->getRequest();
  43.             $currentRoute $request->attributes->get('_route');
  44.             if($request->getRequestUri() === '/js/routing?callback=fos.Router.setData') {
  45.                 throw new NotFoundHttpException();
  46.             }
  47.             //does this routeName NOT in whitelist?
  48.             if(! in_array($currentRoute$this->whiteListOrRouteName)){
  49.                 throw new NotFoundHttpException(sprintf('Route %s is not allowed for orl'$currentRoute));
  50.             }
  51.         }
  52.     }
  53. }