src/Twig/LastNotifications.php line 56

  1. <?php
  2. namespace App\Twig;
  3. use App\Entity\NotificationSystem;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Twig\Environment;
  9. /**
  10.  * Class LastNotifications
  11.  * @package App\Twig
  12.  */
  13. class LastNotifications implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var Environment
  17.      */
  18.     private Environment $twig;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private EntityManagerInterface $manager;
  23.     /**
  24.      * @var TokenStorageInterface
  25.      */
  26.     private TokenStorageInterface $storage;
  27.     /**
  28.      * LastNotifications constructor.
  29.      * @param Environment $twig
  30.      * @param EntityManagerInterface $manager
  31.      * @param TokenStorageInterface $storage
  32.      */
  33.     public function __construct(Environment $twigEntityManagerInterface $managerTokenStorageInterface $storage)
  34.     {
  35.         $this->twig $twig;
  36.         $this->manager $manager;
  37.         $this->storage $storage;
  38.     }
  39.     /**
  40.      * @return array|string[]
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             KernelEvents::CONTROLLER => 'notifications'
  46.         ];
  47.     }
  48.     public function notifications()
  49.     {
  50.         $token $this->storage->getToken();
  51.         if ($token) {
  52.             $user $token->getUser();
  53.             if ($user === 'anon.') {
  54.                 return;
  55.             }
  56.             $notifications $this->manager
  57.                 ->getRepository(NotificationSystem::class)
  58.                 ->getLastThree($user);
  59.             $this->twig->addGlobal('lastNotifications'$notifications);
  60.         }
  61.     }
  62. }