<?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');
}
}
$this->removeFileTmp($_request);
}
/**
* @param $_request
* @throws \Exception
*/
public function removeFileTmp($_request)
{
if (!$_request->isXmlHttpRequest() && !$_request->isMethod('POST')) {
$_directory_tmp = $this->_container->get('kernel')->getProjectDir() . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'tmp';
if (is_dir($_directory_tmp)) {
$_files = glob("$_directory_tmp/*.{xlsx,pdf}", GLOB_BRACE);
foreach ($_files as $_file) {
$_date_now = new \DateTime();
$_date_last_modified = (new \DateTime())->setTimestamp(filemtime($_file));
$_diff_now = $_date_now->diff($_date_last_modified);
if ($_diff_now->i + $_diff_now->h + $_diff_now->d + $_diff_now->m + $_diff_now->y) {
unlink($_file);
}
}
}
}
}
}