src/EventSubscriber/SuspendedClientSubscriber.php line 40
<?phpnamespace App\EventSubscriber;use App\Services\TokenVerifier;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\RouterInterface;class SuspendedClientSubscriber implements EventSubscriberInterface{private TokenVerifier $tokenVerifier;private RouterInterface $router;private RouteCollection $routeCollection;private bool $done = false;/*** @param TokenVerifier $tokenVerifier* @param RouterInterface $router*/public function __construct(TokenVerifier $tokenVerifier, RouterInterface $router){$this->tokenVerifier = $tokenVerifier;$this->router = $router;$this->routeCollection = $router->getRouteCollection();}/*** Redirect to correct locale route if omitted or not concordant with route requirements.** @param ResponseEvent $event* @return void*/public function onKernelResponse(ResponseEvent $event): void{//--------------------------------------------------------------------------------------------------------------// GOAL://--------------------------------------------------------------------------------------------------------------// Redirect all incoming requests to their /locale/route equivalent as long as the route will exists when we do so.// If the route required a specific locale so we force it.// Do nothing if it already has /locale/ in the route to prevent redirect loops//--------------------------------------------------------------------------------------------------------------}/*** @param RequestEvent $event* @return void*/public function onKernelRequest(RequestEvent $event): void{$request = $event->getRequest();if ($this->done === true || !$request->hasPreviousSession() || $this->tokenVerifier->getClient() === null) {return;}if ($this->tokenVerifier->isTokenValid() === false || ($user = $this->tokenVerifier->getClient()) === null) {$this->done = true;$response = new RedirectResponse($this->router->generate('app_login', ['_locale' => $request->getLocale(),]));// Empty data.$this->tokenVerifier->empty();// Add a message to notify that session is expired.$request->getSession()->getFlashBag()->add('danger', "Votre session a expiré.");// Redirect to the login page.$event->setResponse($response);}}public static function getSubscribedEvents(){return [KernelEvents::RESPONSE => [['onKernelResponse', 17]],// must be registered before (i.e. with a higher priority than) the default Locale listenerKernelEvents::REQUEST => [['onKernelRequest', 20]],];}}