src/EventListener/Restrictions/OpeningControlListener.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Restrictions;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. class OpeningControlListener
  6. {
  7.     public function onKernelRequest(RequestEvent $event)
  8.     {
  9.         if (!$event->isMainRequest()) {
  10.             // don't do anything if it's not the master request
  11.             return;
  12.         }
  13.         $request $event->getRequest();
  14.         $routeName $request->attributes->get('_route');
  15.         if (in_array($routeName, ['maintenance_page''closing_page'])) {
  16.             return;
  17.         }
  18.         $allowedIp = [];
  19.         $clientIp $request->getClientIp();
  20.         if (isset($_ENV['ALLOWED_IP']) && $_ENV['ALLOWED_IP']) {
  21.             $allowedIp explode(','$_ENV['ALLOWED_IP']);
  22.         }
  23.         if (false === (bool) $_ENV['RESTRICTION_ACCESS']) {
  24.             return;
  25.         }
  26.         if (in_array($clientIp$allowedIp)) {
  27.             return;
  28.         }
  29.         if (true === (bool) $_ENV['CHECK_OPENING_DATE']) {
  30.             $now = new \DateTime();
  31.             if (new \DateTime($_ENV['OPENING_DATE']) > $now) {
  32.                 $event->setResponse(new RedirectResponse('/maintenance'));
  33.                 return;
  34.             }
  35.             if (new \DateTime($_ENV['ENDING_DATE']) < $now) {
  36.                 $event->setResponse(new RedirectResponse('/fermeture'));
  37.                 return;
  38.             }
  39.         }
  40.         $event->setResponse(new RedirectResponse('/maintenance'));
  41.     }
  42. }