src/FMT/Domain/Listener/UserNotificationListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace FMT\Domain\Listener;
  3. use FMT\Data\Entity\User;
  4. use FMT\Domain\Repository\UserNotificationsRepositoryInterface;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. class UserNotificationListener
  9. {
  10.     private UserNotificationsRepositoryInterface $userNotificationsRepository;
  11.     private SessionInterface $session;
  12.     private TokenStorageInterface $tokenStorage;
  13.     public function __construct(
  14.         UserNotificationsRepositoryInterface $userNotificationsRepository,
  15.         SessionInterface $session,
  16.         TokenStorageInterface $tokenStorage
  17.     ) {
  18.         $this->userNotificationsRepository $userNotificationsRepository;
  19.         $this->session $session;
  20.         $this->tokenStorage $tokenStorage;
  21.     }
  22.     public function onKernelRequest(RequestEvent $event)
  23.     {
  24.         $tokenStorage $this->tokenStorage->getToken();
  25.         if (!$tokenStorage) {
  26.             return;
  27.         }
  28.         /** @var ?User $user */
  29.         $user $tokenStorage->getUser();
  30.         if (!$user instanceof User) {
  31.             return;
  32.         }
  33.         $notifications $this->userNotificationsRepository->findUserNotifications($user);
  34.         foreach ($notifications as $notification) {
  35.             $this->session->getFlashBag()->add('warning'$notification->getMessage());
  36.             $notification->setIsSent(1);
  37.             $this->userNotificationsRepository->add($notification);
  38.             $this->userNotificationsRepository->save($notification);
  39.         }
  40.     }
  41. }