src/EventSubscriber/TwigEventSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\LocationIP\LocationIpInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Twig\Environment;
  8. use App\Manager\InfoManager;
  9. use App\Manager\InfoErrorManager;
  10. use App\Manager\PropertyManager;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use App\Form\OptionsTypeTrait;
  13. class TwigEventSubscriber implements EventSubscriberInterface
  14. {
  15.     use OptionsTypeTrait;
  16.     private $twig;
  17.     private $imgr;
  18.     private $emgr;
  19.     private $pmgr;
  20.     private $param;
  21.     private RequestStack $requestStack;
  22.     public function __construct(
  23.         Environment $twig,
  24.         InfoManager $imgr,
  25.         InfoErrorManager $emgr,
  26.         PropertyManager $pmgr,
  27.         ParameterBagInterface $param,
  28.         RequestStack $requestStack
  29.     ) {
  30.         $this->twig $twig;
  31.         $this->imgr $imgr;
  32.         $this->emgr $emgr;
  33.         $this->pmgr $pmgr;
  34.         $this->param $param;
  35.         $this->requestStack $requestStack;
  36.     }
  37.     public function onControllerEvent(ControllerEvent $event)
  38.     {
  39.         $this->twig->addGlobal('menu_active''');
  40.         $this->twig->addGlobal('info'$this->imgr->find(1));
  41.         $this->twig->addGlobal('info_error'$this->emgr->find(1));
  42.         $this->twig->addGlobal('filter_types'$this->pmgr->listTypesFilter());
  43.         $this->twig->addGlobal('filter_locations'$this->pmgr->listLocationsFilter());
  44.         $this->twig->addGlobal('list_autocomplete'$this->pmgr->listAutocomplete());
  45.         $this->twig->addGlobal('sd_external_paths'$this->external_paths);
  46.         $list_count $this->pmgr->listCountGroupLocation();
  47.         $list_count array_filter($list_count, function ($e) {
  48.             return $e['total'] < 2;
  49.         });
  50.         $list_count array_column($list_count'sdurl''sdkey');
  51.         $this->twig->addGlobal('sdlistcount'$list_count);
  52.         $validations = [
  53.             'dni' => [
  54.                 'endpoint' => $this->param->get('valid_dni_endpoint'),
  55.                 'token' => $this->param->get('valid_dni_token'),
  56.             ],
  57.             'email' => [
  58.                 'endpoint' => $this->param->get('valid_email_endpoint'),
  59.                 'apikey' => $this->param->get('valid_email_key'),
  60.             ]
  61.         ];
  62.         $this->twig->addGlobal('validations'$validations);
  63.         $this->twig->addGlobal('show_regionalization_script'$this->showRegionalizationScript());
  64.     }
  65.     public static function getSubscribedEvents(): array
  66.     {
  67.         return [
  68.             ControllerEvent::class => 'onControllerEvent',
  69.         ];
  70.     }
  71.     protected function showRegionalizationScript(): bool
  72.     {
  73.         $region $this->requestStack->getSession()->get(LocationIpInterface::REGION);
  74.         $clear = (bool) $this->requestStack->getSession()->get(LocationIpInterface::CLEARfalse);
  75.         $invalid = (bool) $this->requestStack->getSession()->get(LocationIpInterface::INVALIDfalse);
  76.         if ($clear or $invalid) {
  77.             return false;
  78.         }
  79.         if (!is_null($region)) {
  80.             return false;
  81.         }
  82.         return true;
  83.     }
  84. }