<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Controller\SessionValidController;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* @see : https://symfony.com/doc/current/event_dispatcher/before_after_filters.html
*/
class CheckSessionValidity implements EventSubscriberInterface
{
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
private $request;
public function __construct(RequestStack $request_stack)
{
$this->request = $request_stack->getCurrentRequest();
}
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();
// when a controller class defines multiple action methods, the controller
// is returned as [$controllerInstance, 'methodName']
if (is_array($controller)) {
$controller = $controller[0];
}
if ($controller instanceof SessionValidController) {
// If we have the flag indicating that the tunnel is over,
// empty the session to prevent going back to previous steps
if($this->request->getSession()->get('finished')) {
// Invalidates the current session
$this->request->getSession()->invalidateNamespace($this->request->query->get('_ts'));
// Any attemp to go back to previous steps will end up here.
/* NOTE : we do not need to to this, any controller will check
* session validity and we have invalidate the session,
* so we will by automatically redirected to the start of the tunnel
$event->setController(function() {
return new RedirectResponse('https://www.maisonsduvoyage.com/');
});
*/
}
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}