src/EventSubscriber/CommentSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Order;
  4. use App\Entity\Step;
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Twig\Environment;
  13. class CommentSubscriber implements EventSubscriberInterface
  14. {
  15.     private Environment $twig;
  16.     private EntityManagerInterface $em;
  17.     private TokenStorageInterface $tokenStorage;
  18.     public function __construct(
  19.         Environment $twig,
  20.         EntityManagerInterface $em,
  21.         TokenStorageInterface $tokenStorage
  22.     ) {
  23.         $this->twig $twig;
  24.         $this->em $em;
  25.         $this->tokenStorage $tokenStorage;
  26.     }
  27.     public function onKernelRequest(RequestEvent $event)
  28.     {
  29.         if (!$event->isMasterRequest()) {
  30.             return false;
  31.         }
  32.         $token $this->tokenStorage->getToken();
  33.         if ($token === null) {
  34.             return false;
  35.         }
  36.         /** @var User $user */
  37.         $user $token->getUser();
  38.         if ($user instanceof UserInterface and ($user->isContributor() or $user->isEmployee())) {
  39.             if ($user->isContributor()) {
  40.                 /** @var Order[] $orders */
  41.                 $orders $this->em->getRepository(Order::class)->findWaitingContribution($user);
  42.             } else {
  43.                 $filter = [];
  44.                 $filter['step'] = $this->em->getRepository(Step::class)->findOneBy(['constName' => Step::MAKING]);
  45.                 /** @var Order[] $orders */
  46.                 $orders $this->em->getRepository(Order::class)->findByEmployeeFilter($user$filter);
  47.             }
  48.             $commentsNotRead = [];
  49.             foreach ($orders as $order) {
  50.                 if ($order->getCreation() and $order->getCurrentStep() and $order->getCurrentStep()->getStep()->getConstName() == Step::MAKING) {
  51.                     foreach ($order->getCreation()->getCreationComments() as $creationComment) {
  52.                         if (!$user->getCommentsRead()->contains($creationComment)) {
  53.                             $commentsNotRead[$creationComment->getCreatedAt()->format('YmdHis')] = [
  54.                                 'order' => $order,
  55.                                 'comment' => $creationComment,
  56.                             ];
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.             rsort($commentsNotRead);
  62.             $this->twig->addGlobal('comments_not_read'$commentsNotRead);
  63.         }
  64.     }
  65.     public static function getSubscribedEvents(): array
  66.     {
  67.         return array(
  68.             KernelEvents::REQUEST => 'onKernelRequest',
  69.         );
  70.     }
  71. }