<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class EmpresaListener implements EventSubscriberInterface
{
private $twig;
private $security;
public function __construct(Environment $twig, Security $security)
{
$this->twig = $twig;
$this->security = $security;
}
public function onKernelController(ControllerEvent $event)
{
$user = $this->security->getUser();
if ($user instanceof UserInterface) {
$empresaId = $user->getEmpresaId();
} else {
$empresaId = null;
}
$this->twig->addGlobal('empresaId', $empresaId);
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}