src/Listener/WhiteLabelListener.php line 40

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