<?php
// src/EventSubscriber/TwigUserNotificationSubscriber.php
namespace App\EventSubscriber;
use App\Repository\TicketReplyRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class TwigUserNotificationSubscriber implements EventSubscriberInterface
{
private $twig;
private $ticketReplyRepository;
private $security;
public function __construct(Environment $twig, TicketReplyRepository $ticketReplyRepository, Security $security)
{
$this->twig = $twig;
$this->ticketReplyRepository = $ticketReplyRepository;
$this->security = $security;
}
public function onKernelController(ControllerEvent $event)
{
// Sicherstellen, dass der Benutzer authentifiziert ist
$user = $this->security->getUser();
if ($user !== null) {
// Zähle ungelesene Ticket-Antworten für den Benutzer
$userUnreadRepliesCount = count($this->ticketReplyRepository->findUnreadRepliesForUser($user->getId()));
// Setze die globale Variable in Twig (klar benannt für den User-Kontext)
$this->twig->addGlobal('user_unreadRepliesCount', $userUnreadRepliesCount);
} else {
// Wenn kein Benutzer authentifiziert ist, setze die Variable auf 0
$this->twig->addGlobal('user_unreadRepliesCount', 0);
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}