src/EventListener/MaintenanceListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\VignetteMaintenance\VignetteMaintenance;
  4. use App\Service\SystemService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  10. class MaintenanceListener
  11. {
  12.     protected $request;
  13.     protected $entityManager;
  14.     protected $container;
  15.     public function __construct(RequestStack $requestStackEntityManagerInterface $entityManagerContainerInterface $container)
  16.     {
  17.         $this->request $requestStack->getCurrentRequest();
  18.         $this->entityManager $entityManager;
  19.         $this->container $container;
  20.     }
  21.     public function onKernelRequest(GetResponseEvent $event)
  22.     {
  23.         $maintenanceRepository $this->entityManager->getRepository(VignetteMaintenance::class);
  24.         $maintenance $maintenanceRepository->findLastMaintenance();
  25.         if ($maintenance) {
  26.             $currentDate = new \DateTime();
  27.             $route $this->request->get('_route');
  28.             if ($maintenance->getDateFrom() < $currentDate && $maintenance->getDateTo() > $currentDate) {
  29.                 if (!SystemService::isAdminPath($route) && !SystemService::isIpnRoute($route)) {
  30.                     $templating $this->container->get('templating');
  31.                     $render $templating->render('@templates/Front/Shop/maintenance.html.twig', [
  32.                         'maintenance' => $maintenance
  33.                     ]);
  34.                     $event->setResponse(new Response($renderResponse::HTTP_SERVICE_UNAVAILABLE));
  35.                     $event->stopPropagation();
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }