src/EventSubscriber/NotificationSubcriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Twig\Environment;
  4. use App\Utils\NotificationMenu;
  5. use App\Repository\NotificationRepository;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Entity\Notification;
  12. class NotificationSubcriber implements EventSubscriberInterface
  13. {
  14.     const MENUURL=['app_order_detail','app_ticket_details'];
  15.     private $twig;
  16.     private $notificationRepository;
  17.     private $notificationMenu;
  18.     private $entityManager;
  19.     public function __construct(Environment $twig,
  20.         NotificationRepository $notificationRepository,
  21.         NotificationMenu $notificationMenu,
  22.         ManagerRegistry $doctrine
  23.         )
  24.     {
  25.        $this->twig=$twig;
  26.        $this->notificationRepository=$notificationRepository;
  27.        $this->notificationMenu=$notificationMenu;
  28.        $this->entityManager $doctrine->getManager();
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         // return the subscribed events, their methods and priorities
  33.         return [
  34.             KernelEvents::REQUEST => [
  35.                 ['notifyRequest'0],
  36.             ],
  37.             
  38.         ];
  39.     }
  40.     public function notifyRequest(RequestEvent $event)
  41.     {
  42.         $route=$event->getRequest()->attributes->get('_route');
  43.         if (in_array($routeSELF::MENUURL)) {
  44.             $id=$event->getRequest()->attributes->get('notif');
  45.             $notifObject=null;
  46.             if(!is_null($id)){
  47.                 $notifObject=$this->notificationRepository->find($id); 
  48.             }
  49.             
  50.         
  51.             if($notifObject instanceof Notification){
  52.                 $notifObject->setStatus(1);
  53.                 $this->entityManager->persist($notifObject);
  54.                 $this->entityManager->flush();
  55.             }
  56.             
  57.         }
  58.        
  59.         $notifs=$this->notificationRepository->findBy(['direction'=>'agent'],['createdAt'=>'DESC'],6);
  60.         $notifCount=count($this->notificationRepository->findBy(['direction'=>'agent','status'=>0]));
  61.         $menu=$this->notificationMenu->buildNotifMenu($notifs);
  62.        
  63.         $this->twig->addGlobal('menu',$menu);
  64.         $this->twig->addGlobal('notifCount',$notifCount);
  65.     }
  66.     
  67. }