<?php
namespace App\EventSubscriber;
use App\Service\NotificationService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Twig\Environment;
class NotificationSubscriber implements EventSubscriberInterface
{
public function __construct(
private Environment $twig,
private NotificationService $notificationService,
private TokenStorageInterface $tokenStorage,
) {
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
public function onKernelController(ControllerEvent $event): void
{
if ($event->getRequest()->isXmlHttpRequest()) {
return;
}
$user = $this->tokenStorage->getToken()?->getUser();
if ($user) {
$this->twig->addGlobal('notification_service', $this->notificationService);
}
}
}