<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
class MaintenanceListener
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(RequestEvent $event)
{
$date = new \DateTime();
$dateStart = new \DateTime($this->container->getParameter('maintenance_start'));
$dateEnd = new \DateTime($this->container->getParameter('maintenance_end'));
if ($date->format("Ymdhis") >= $dateStart->format("Ymdhis") && $date->format("Ymdhis") <= $dateEnd->format("Ymdhis")) {
$engine = $this->container->get('templating');
$template = $engine->render('::maintenance.html.twig', array('dateEnd' => $dateEnd));
// We send our response with a 503 response code (service unavailable)
$event->setResponse(new Response($template, 503));
$event->stopPropagation();
}
}
}