src/Listener/MessagingListener.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Controller\MessengerController;
  4. use App\Entity\Messaging\Message;
  5. use App\Entity\Messaging\Request;
  6. use App\Entity\User;
  7. use App\Service\Mailer;
  8. use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\ORM\Event\LifecycleEventArgs;
  11. use Doctrine\ORM\Events;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  15. class MessagingListener implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var TokenStorageInterface
  19.      */
  20.     private $storage;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $em;
  25.     /**
  26.      * @var Mailer
  27.      */
  28.     private $mailer;
  29.     public function __construct(TokenStorageInterface $storageEntityManagerInterface $emMailer $mailer)
  30.     {
  31.         $this->storage $storage;
  32.         $this->em $em;
  33.         $this->mailer $mailer;
  34.     }
  35.     public function getSubscribedEvents()
  36.     {
  37.         return [Events::prePersist];
  38.     }
  39.     private function setLatestMessage(Message $message)
  40.     {
  41.         $message->getRequest()->setLatest($message);
  42.         $md $this->em->getClassMetadata(Request::class);
  43.         $this->em->getUnitOfWork()->computeChangeSet($md$message->getRequest());
  44.     }
  45.     public function prePersist(LifecycleEventArgs $args)
  46.     {
  47.         $entity $args->getEntity();
  48.         if ($entity instanceof Message) {
  49.             $this->setLatestMessage($entity);
  50.             if (!$entity->shouldNotify()) {
  51.                 return;
  52.             }
  53.             // Send a notification email
  54.             if ($entity->getAuthor()->isStaff()) {
  55.                 $user $entity->getRequest()->getAuthor() === $entity->getAuthor()
  56.                     ? $entity->getRequest()->getRecipient()
  57.                     : $entity->getRequest()->getAuthor();
  58.                 if (!$user->getSettings()->isMessengerNotificationSent()) {
  59.                     $user->getSettings()->setMessengerNotificationSent(true);
  60.                     $email $this->mailer->template($user'messenger-notification');
  61.                     $this->mailer->send($email);
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     public function onKernelController(ControllerEvent $event)
  67.     {
  68.         $controller $event->getController();
  69.         if (!is_array($controller)) {
  70.             return;
  71.         }
  72.         if (!$controller[0] instanceof MessengerController) {
  73.             return;
  74.         }
  75.         // Reset notification lock
  76.         $user $this->storage->getToken()->getUser();
  77.         if (!$user instanceof User) {
  78.             return;
  79.         }
  80.         if (!$user->getSettings()->isMessengerNotificationSent()) {
  81.             return;
  82.         }
  83.         $user->getSettings()->setMessengerNotificationSent(false);
  84.         $this->em->flush();
  85.     }
  86.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  87.     {
  88.         // Reset notification lock
  89.         /** @var User $user */
  90.         $user $event->getAuthenticationToken()->getUser();
  91.         if (!$user->getSettings()->isMessengerNotificationSent()) {
  92.             return;
  93.         }
  94.         $user->getSettings()->setMessengerNotificationSent(false);
  95.         $this->em->flush();
  96.     }
  97. }