src/EventSubscriber/EmpresaListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Twig\Environment;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class EmpresaListener implements EventSubscriberInterface
  10. {
  11.     private $twig;
  12.     private $security;
  13.     public function __construct(Environment $twigSecurity $security)
  14.     {
  15.         $this->twig $twig;
  16.         $this->security $security;
  17.     }
  18.     public function onKernelController(ControllerEvent $event)
  19.     {
  20.         $user $this->security->getUser();
  21.         if ($user instanceof UserInterface) {
  22.             $empresaId $user->getEmpresaId();
  23.         } else {
  24.             $empresaId null;
  25.         }
  26.         $this->twig->addGlobal('empresaId'$empresaId);
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             KernelEvents::CONTROLLER => 'onKernelController',
  32.         ];
  33.     }
  34. }