<?php
namespace App\Listener;
use App\Service\WhiteLabelService;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class WhiteLabelListener
{
private LoggerInterface $logger;
private WhiteLabelService $whiteLabelService;
// route name only not path
private array $authorizedRouteName = [
'whitelabel_homepage',
'duplicated_prospect_management',
'prospect_duplicate_post_management',
'lp_form_success',
'rgpd',
'cgu',
'privacy',
'legal-notice',
'phone_validation_confirmation',
'index_whitelabel_lp1',
'index_whitelabel_lp2',
'liip_imagine_filter'
];
public function __construct(LoggerInterface $logger, WhiteLabelService $whiteLabelService)
{
$this->logger = $logger;
$this->whiteLabelService = $whiteLabelService;
}
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 whitelabel app
if($this->whiteLabelService->isWhiteLabel()){
//check route name
$currentRoute = $request->attributes->get('_route');
//does this routeName NOT in whitelist?
if(! in_array($currentRoute, $this->authorizedRouteName)){
throw new NotFoundHttpException(sprintf('Route %s is not allowed for whitelabel', $currentRoute));
}
}
}
}