src/EventListener/Web/MaintenanceModeListener.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Web;
  3. use App\Application\Service\Helper\ConfigurationService;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Twig\Environment;
  7. class MaintenanceModeListener
  8. {
  9.     /** @var Environment */
  10.     private $twig;
  11.     /** @var bool */
  12.     private $maintenanceMode;
  13.     /** @var ConfigurationService */
  14.     private $configurationService;
  15.     public function __construct(bool $maintenanceModeEnvironment $twigConfigurationService $configurationService)
  16.     {
  17.         $this->maintenanceMode $maintenanceMode;
  18.         $this->twig $twig;
  19.         $this->configurationService $configurationService;
  20.     }
  21.     public function onKernelRequest(RequestEvent $requestEvent): void
  22.     {
  23.         if ($this->maintenanceMode) {
  24.             $allowedIps explode(';'$this->configurationService->getCachedValue('MAINTENANCE_IPS'));
  25.             $requestIp $requestEvent->getRequest()->getClientIp();
  26.             if (!in_array($requestIp$allowedIpstrue)) {
  27.                 $renderedTemplate $this->twig->render('front/maintenance/maintenance.html.twig');
  28.                 $response = new Response($renderedTemplate);
  29.                 $requestEvent->setResponse($response);
  30.             }
  31.         }
  32.     }
  33. }