<?php
namespace LogistMaintenanceBundle\EventListener;
use Symfony\Component\HttpFoundation\Response;
use LogistMaintenanceBundle\Service\LogistMaintenancePlaceholderService;
class MaintenanceListener
{
const MAINTENANCE_ERROR_CODE = 503;
/**
* @var boolean
*/
protected $isTriggered = FALSE;
/**
* @var LogistMaintenancePlaceholderService
*/
protected $placeholderService;
/**
* @param LogistMaintenancePlaceholderService $placeholderService
*/
public function __construct(LogistMaintenancePlaceholderService $placeholderService)
{
$this->placeholderService = $placeholderService;
}
public function onKernelRequest($event)
{
if(
($this->placeholderService->getMode() === LogistMaintenancePlaceholderService::MODE_ENABLED)
&& !$this->isTriggered // to prevent endless loop with render controller in template
){
$this->isTriggered = TRUE;
$body = $this->placeholderService->renderPlaceholder();
$response = new Response(
$body,
LogistMaintenancePlaceholderService::MAINTENANCE_ERROR_CODE,
['Content-Type' => 'text/html']
);
$event->setResponse($response);
$event->stopPropagation();
}
}
}