src/EventSubscriber/CheckSessionValidity.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use App\Controller\SessionValidController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. /**
  10.  * @see : https://symfony.com/doc/current/event_dispatcher/before_after_filters.html
  11.  */
  12. class CheckSessionValidity implements EventSubscriberInterface
  13. {
  14.    /**
  15.     * @var \Symfony\Component\HttpFoundation\Request
  16.     */
  17.     private $request;
  18.     public function __construct(RequestStack $request_stack)
  19.     {
  20.         $this->request $request_stack->getCurrentRequest();
  21.     }
  22.     public function onKernelController(ControllerEvent $event)
  23.     {
  24.         $controller $event->getController();
  25.         // when a controller class defines multiple action methods, the controller
  26.         // is returned as [$controllerInstance, 'methodName']
  27.         if (is_array($controller)) {
  28.             $controller $controller[0];
  29.         }
  30.         if ($controller instanceof SessionValidController) {
  31.           // If we have the flag indicating that the tunnel is over,
  32.           // empty the session to prevent going back to previous steps
  33.           if($this->request->getSession()->get('finished')) {
  34.               // Invalidates the current session
  35.               $this->request->getSession()->invalidateNamespace($this->request->query->get('_ts'));
  36.               // Any attemp to go back to previous steps will end up here.
  37.               /* NOTE : we do not need to to this, any controller will check
  38.                *        session validity and we have invalidate the session,
  39.                *        so we will by automatically redirected to the start of the tunnel
  40.               $event->setController(function() {
  41.                   return new RedirectResponse('https://www.maisonsduvoyage.com/');
  42.               });
  43.               */
  44.           }
  45.         }
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             KernelEvents::CONTROLLER => 'onKernelController',
  51.         ];
  52.     }
  53. }