src/Handler/MaintenanceHandler.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Handler;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. class MaintenanceHandler
  8. {
  9.     private $container;
  10.     private $tokenStorage;
  11.     public function __construct(ContainerInterface $containerTokenStorageInterface $tokenStorage)
  12.     {
  13.         $this->container $container;
  14.         $this->tokenStorage $tokenStorage;
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         //$maintenanceUntil = $this->container->hasParameter('underMaintenanceUntil') ? $this->container->getParameter('underMaintenanceUntil') : false;
  19.         $maintenance $this->container->hasParameter('maintenance') ? $this->container->getParameter('maintenance') : false;
  20.         //$test = $this->container->get('security.token_storage');
  21.         //$debug = in_array($this->container->get('kernel')->getEnvironment(), array('test', 'dev'));
  22.         //if ($maintenance && !$debug) {
  23.         if ($maintenance) {
  24.             try {
  25.                 $user $this->tokenStorage->getToken()->getUser();
  26.                 $isSuperAdmin $user->getUserRole() == 'ROLE_SUPER_ADMIN' true false;
  27.                 if (!$isSuperAdmin) {
  28.                     $engine $this->container->get('twig');
  29.                     $content $engine->render('maintenance.html.twig');
  30.                     $event->setResponse(new Response($content503));
  31.                     $event->stopPropagation();
  32.                 }
  33.             } catch (\Throwable $th) {
  34.                 //qui ci entra quando non esiste il token di autenticazione, se non esiste vado avanti, perché si mostra il login
  35.                 //$event->stopPropagation();
  36.             }
  37.         }
  38.     }
  39. }