<?php
namespace App\Listener;
use App\Service\OrlService;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class OrlListener
{
private $logger;
private $orlService;
// route name only not path
private $whiteListOrRouteName = [
'orl_homepage',
'orl_finder',
'add_prospect_on_orl_office',
'orl_contact',
'orl_contact_send',
'orl_legal_notice',
'orl_privacy',
'orl_cgu'
];
public function __construct(LoggerInterface $logger, OrlService $orlService)
{
$this->logger = $logger;
$this->orlService = $orlService;
}
public function __invoke(RequestEvent $event): void
{
$request = $event->getRequest();
// ignore FOS JS routing init query
if($request->getRequestUri() === '/js/routing?callback=fos.Router.setData') {
return;
}
// Don't process events with HTTP exceptions - those have either been thrown
// by us or have nothing to do with this module.
if ($event->getRequest()->get('exception') != NULL) {
return;
}
// are we in a orl app
if($this->orlService->isOrlWebsite()){
//check route name
$request = $event->getRequest();
$currentRoute = $request->attributes->get('_route');
if($request->getRequestUri() === '/js/routing?callback=fos.Router.setData') {
throw new NotFoundHttpException();
}
//does this routeName NOT in whitelist?
if(! in_array($currentRoute, $this->whiteListOrRouteName)){
throw new NotFoundHttpException(sprintf('Route %s is not allowed for orl', $currentRoute));
}
}
}
}