<?php
namespace App\EventListener;
use App\Entity\VignetteMaintenance\VignetteMaintenance;
use App\Service\SystemService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class DailyMaintenanceListener
{
protected $request;
protected $entityManager;
protected $container;
public function __construct(RequestStack $requestStack, EntityManagerInterface $entityManager, ContainerInterface $container)
{
$this->request = $requestStack->getCurrentRequest();
$this->entityManager = $entityManager;
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
$currentDate = new \DateTime();
$startOfMaintenanceTime = new \DateTime();
$startOfMaintenanceTime->setTime(23,55,00);
$endOfMaintenanceTime = new \DateTime();
$endOfMaintenanceTime->add(new \DateInterval('P1D'));
$endOfMaintenanceTime->setTime(00,05,00);
$route = $this->request->get('_route');
if ($startOfMaintenanceTime <= $currentDate && $endOfMaintenanceTime >= $currentDate) {
if (!SystemService::isAdminPath($route) && !SystemService::isIpnRoute($route)) {
$templating = $this->container->get('templating');
$render = $templating->render('@templates/Front/Shop/daily-maintenance.html.twig');
$event->setResponse(new Response($render, Response::HTTP_SERVICE_UNAVAILABLE));
$event->stopPropagation();
}
}
}
}