src/EventSubscriber/NotificationUrlSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\NotificationRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Security\Core\Security;
  9. class NotificationUrlSubscriber implements EventSubscriberInterface
  10. {
  11.     private $security;
  12.     private $notificationRepo;
  13.     private $em;
  14.     public function __construct(Security $securityNotificationRepository $notificationRepoEntityManagerInterface $em)
  15.     {
  16.         $this->security $security;
  17.         $this->notificationRepo $notificationRepo;
  18.         $this->em $em;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             KernelEvents::REQUEST => ['onKernelRequest'5],
  24.         ];
  25.     }
  26.     public function onKernelRequest(RequestEvent $event)
  27.     {
  28.         if (!$event->isMainRequest())
  29.             return;
  30.         $user $this->security->getUser();
  31.         if (!$user)
  32.             return;
  33.         $currentUri $event->getRequest()->getRequestUri();
  34.         $notifs $this->notificationRepo->createQueryBuilder('n')
  35.             ->where('n.user = :user')
  36.             ->andWhere('n.lu = false')
  37.             ->andWhere('n.lien = :lien')
  38.             ->setParameter('user'$user)
  39.             ->setParameter('lien'$currentUri)
  40.             ->getQuery()
  41.             ->getResult();
  42.         foreach ($notifs as $notif) {
  43.             $notif->setLu(true);
  44.         }
  45.         if (!empty($notifs)) {
  46.             $this->em->flush();
  47.         }
  48.     }
  49. }