src/ProPneu/Service/MetierManagerBundle/EventListener/PpRequestListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\ProPneu\Service\MetierManagerBundle\EventListener;
  3. use App\ProPneu\Service\MetierManagerBundle\Utils\EntityName;
  4. use App\ProPneu\Service\MetierManagerBundle\Utils\ServiceName;
  5. use Symfony\Component\DependencyInjection\Container;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\Routing\RouterInterface;
  10. class PpRequestListener
  11. {
  12.     private $_router;
  13.     private $_container;
  14.     /**
  15.      * RequestListener constructor.
  16.      * @param RouterInterface $_router
  17.      * @param Container $_container
  18.      */
  19.     public function __construct(RouterInterface $_routerContainer $_container)
  20.     {
  21.         $this->_router    $_router;
  22.         $this->_container $_container;
  23.     }
  24.     /**
  25.      * @param GetResponseEvent $_event
  26.      * @throws \Exception
  27.      */
  28.     public function onRequest(GetResponseEvent $_event)
  29.     {
  30.         // Recuperer manager
  31.         $_utils_manager $this->_container->get(ServiceName::SRV_METIER_UTILS);
  32.         $_request           $_event->getRequest();
  33.         $_branche_paramater $_request->get('_branche');
  34.         $_context           $this->_router->getContext();
  35.         if (!is_null($_branche_paramater)) {
  36.             $_user_connected $_utils_manager->getUserConnected();
  37.             $_branche_name   $_utils_manager->findOneEntityByFilter(EntityName::PP_BRANCHE, [
  38.                 'brcLibelle' => $_branche_paramater
  39.             ]);
  40.             if (is_object($_user_connected) && $_branche_name) {
  41.                 $this->_container->get('session')->set('current_branche'$_branche_name->getBrcLibelle());
  42.                 $_current_branche_id $_branche_name->getId();
  43.                 $_user_branche_id    $_user_connected->getPpBranche() ? $_user_connected->getPpBranche()->getId() : 0;
  44.                 if ($_current_branche_id != $_user_branche_id) {
  45.                     $_response = new RedirectResponse($this->_router->generate('eternaly_logout'));
  46.                     $_event->setResponse($_response);
  47.                 }
  48.                 $_context->setParameter('_branche'$_branche_name->getBrcLibelle());
  49.                 $this->_container->get('session')->set('current_branche'$_branche_name->getBrcLibelle());
  50.             } else if (is_object($_branche_name)) {
  51.                 $this->_container->get('session')->set('current_branche'$_branche_name->getBrcLibelle());
  52.                 $_context->setParameter('_branche'$_branche_name->getBrcLibelle());
  53.                 return;
  54.             } else {
  55.                 throw new NotFoundHttpException('404 not found');
  56.             }
  57.         }
  58.         $this->removeFileTmp($_request);
  59.     }
  60.     /**
  61.      * @param $_request
  62.      * @throws \Exception
  63.      */
  64.     public function removeFileTmp($_request)
  65.     {
  66.         if (!$_request->isXmlHttpRequest() && !$_request->isMethod('POST')) {
  67.             $_directory_tmp $this->_container->get('kernel')->getProjectDir() . DIRECTORY_SEPARATOR 'public' DIRECTORY_SEPARATOR 'upload' DIRECTORY_SEPARATOR 'tmp';
  68.             if (is_dir($_directory_tmp)) {
  69.                 $_files glob("$_directory_tmp/*.{xlsx,pdf}"GLOB_BRACE);
  70.                 foreach ($_files as $_file) {
  71.                     $_date_now           = new \DateTime();
  72.                     $_date_last_modified = (new \DateTime())->setTimestamp(filemtime($_file));
  73.                     $_diff_now           $_date_now->diff($_date_last_modified);
  74.                     if ($_diff_now->$_diff_now->$_diff_now->$_diff_now->$_diff_now->y) {
  75.                         unlink($_file);
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }