src/Modules/Notification/Event/Subscriber/SendNewChatMessageNotification.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Notification\Event\Subscriber;
  3. use App\Modules\Chat\Event\MessageSent;
  4. use App\Modules\Chat\Service\MessagesService;
  5. use App\Modules\Notification\Entity\NotificationEntity;
  6. use App\Modules\Notification\Message\System\YouHasNewChatMessageNotification;
  7. use App\Modules\Notification\Service\NotificationService;
  8. use App\Service\WebRoutes;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class SendNewChatMessageNotification implements EventSubscriberInterface
  13. {
  14.     private $notificationService;
  15.     private $chatMessagesService;
  16.     private $logger;
  17.     public function __construct(NotificationService $notificationServiceMessagesService $chatMessagesServiceLoggerInterface $logger)
  18.     {
  19.         $this->notificationService $notificationService;
  20.         $this->chatMessagesService $chatMessagesService;
  21.         $this->logger $logger;
  22.     }
  23.     /**
  24.      * Returns an array of event names this subscriber wants to listen to.
  25.      *
  26.      * The array keys are event names and the value can be:
  27.      *
  28.      *  * The method name to call (priority defaults to 0)
  29.      *  * An array composed of the method name to call and the priority
  30.      *  * An array of arrays composed of the method names to call and respective
  31.      *    priorities, or 0 if unset
  32.      *
  33.      * For instance:
  34.      *
  35.      *  * ['eventName' => 'methodName']
  36.      *  * ['eventName' => ['methodName', $priority]]
  37.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  38.      *
  39.      * @return array The event names to listen to
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             MessageSent::class => ['sendNotification']
  45.         ];
  46.     }
  47.     public function sendNotification(MessageSent $event)
  48.     {
  49.         try {
  50.             $message $this->chatMessagesService->get($event->getChatId(), $event->getChatMessageId());
  51.             $author  $message->getAuthor();
  52.             $chat    $message->getChat();
  53.             $contract $chat->getContract();
  54.             $systemNotificationReceivers = new ArrayCollection();
  55.             foreach ($chat->getParticipants() as $participant) {
  56.                 if ($participant != $author) {
  57.                     $systemNotificationReceivers->add($participant);
  58.                 }
  59.             }
  60.             $link = [
  61.                 'id' => $chat->getId(),
  62.                 'entity' => NotificationEntity::CHAT,
  63.                 'message' => $message->getContent(),
  64.                 'contract_ref' => !is_null($contract) ? $contract->getRef() : ''
  65.             ];
  66.             $message = new YouHasNewChatMessageNotification($link$author$systemNotificationReceivers);
  67.             $this->notificationService->sendSystem($message);
  68.         } catch (\Exception $e) {
  69.             $this->logger->error($e->getMessage());
  70.         }
  71.     }
  72. }