<?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 MaintenanceListener
{
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)
{
$maintenanceRepository = $this->entityManager->getRepository(VignetteMaintenance::class);
$maintenance = $maintenanceRepository->findLastMaintenance();
if ($maintenance) {
$currentDate = new \DateTime();
$route = $this->request->get('_route');
if ($maintenance->getDateFrom() < $currentDate && $maintenance->getDateTo() > $currentDate) {
if (!SystemService::isAdminPath($route) && !SystemService::isIpnRoute($route)) {
$templating = $this->container->get('templating');
$render = $templating->render('@templates/Front/Shop/maintenance.html.twig', [
'maintenance' => $maintenance
]);
$event->setResponse(new Response($render, Response::HTTP_SERVICE_UNAVAILABLE));
$event->stopPropagation();
}
}
}
}
}