<?php
namespace App\ProPneu\Service\MetierManagerBundle\EventListener;
use App\ProPneu\Service\MetierManagerBundle\Utils\EntityName;
use App\ProPneu\Service\MetierManagerBundle\Utils\ServiceName;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
class PpRequestListener
{
private $_router;
private $_container;
/**
* RequestListener constructor.
* @param RouterInterface $_router
* @param Container $_container
*/
public function __construct(RouterInterface $_router, Container $_container)
{
$this->_router = $_router;
$this->_container = $_container;
}
/**
* @param GetResponseEvent $_event
* @throws \Exception
*/
public function onRequest(GetResponseEvent $_event)
{
// Recuperer manager
$_utils_manager = $this->_container->get(ServiceName::SRV_METIER_UTILS);
$_request = $_event->getRequest();
$_branche_paramater = $_request->get('_branche');
$_context = $this->_router->getContext();
if (!is_null($_branche_paramater)) {
$_user_connected = $_utils_manager->getUserConnected();
$_branche_name = $_utils_manager->findOneEntityByFilter(EntityName::PP_BRANCHE, [
'brcLibelle' => $_branche_paramater
]);
if (is_object($_user_connected) && $_branche_name) {
$this->_container->get('session')->set('current_branche', $_branche_name->getBrcLibelle());
$_current_branche_id = $_branche_name->getId();
$_user_branche_id = $_user_connected->getPpBranche() ? $_user_connected->getPpBranche()->getId() : 0;
if ($_current_branche_id != $_user_branche_id) {
$_response = new RedirectResponse($this->_router->generate('eternaly_logout'));
$_event->setResponse($_response);
}
$_context->setParameter('_branche', $_branche_name->getBrcLibelle());
$this->_container->get('session')->set('current_branche', $_branche_name->getBrcLibelle());
} else if (is_object($_branche_name)) {
$this->_container->get('session')->set('current_branche', $_branche_name->getBrcLibelle());
$_context->setParameter('_branche', $_branche_name->getBrcLibelle());
return;
} else {
throw new NotFoundHttpException('404 not found');
}
}
}
}