<?php
// App/Handler/SessionIdleHandler.php
namespace App\Handler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class SessionIdleHandler
{
protected $session;
protected $tokenStorage;
protected $router;
protected $maxIdleTime;
public function __construct(
SessionInterface $session,
TokenStorageInterface $tokenStorage,
RouterInterface $router,
$maxIdleTime = 0
) {
$this->session = $session;
$this->tokenStorage = $tokenStorage;
$this->router = $router;
$this->maxIdleTime = $maxIdleTime;
}
public function onKernelRequest(RequestEvent $event) : void
{
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
if ($this->maxIdleTime > 0) {
$this->session->start();
$lapse = time() - $this->session->getMetadataBag()->getLastUsed();
if ($lapse > $this->maxIdleTime && null !== $this->tokenStorage->getToken()) {
if($event->setResponse(new RedirectResponse($this->router->generate('logoutUser', array('from' => 1)))))
{
$this->tokenStorage->setToken(null);
}
}
}
}
}