src/Handler/SessionIdleHandler.php line 27

Open in your IDE?
  1. <?php
  2. // App/Handler/SessionIdleHandler.php
  3. namespace App\Handler;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\HttpKernelInterface;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. class SessionIdleHandler
  11. {
  12.     protected $session;
  13.     protected $tokenStorage;
  14.     protected $router;
  15.     protected $maxIdleTime;
  16.     public function __construct(
  17.         SessionInterface $session,
  18.         TokenStorageInterface $tokenStorage,
  19.         RouterInterface $router,
  20.         $maxIdleTime 0
  21.     ) {
  22.         $this->session $session;
  23.         $this->tokenStorage $tokenStorage;
  24.         $this->router $router;
  25.         $this->maxIdleTime $maxIdleTime;
  26.     }
  27.     public function onKernelRequest(RequestEvent $event) : void
  28.     {
  29.         if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
  30.             return;
  31.         }
  32.         if ($this->maxIdleTime 0) {
  33.             $this->session->start();
  34.             $lapse time() - $this->session->getMetadataBag()->getLastUsed();
  35.             if ($lapse $this->maxIdleTime && null !== $this->tokenStorage->getToken()) {
  36.                 if($event->setResponse(new RedirectResponse($this->router->generate('logoutUser', array('from' => 1)))))
  37.                 {
  38.                   $this->tokenStorage->setToken(null);
  39.                 }
  40.             }
  41.         }
  42.     }
  43. }