src/Listener/NewCommentSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Event\NewCommentEvent;
  4. use App\Manager\NotificationManager;
  5. use App\Manager\UserRightsManagerInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9.  * Class NewCommentSubscriber
  10.  */
  11. class NewCommentSubscriber implements EventSubscriberInterface
  12. {
  13.     public const COMMENT_POSTED_DONE 'comment.posted.done';
  14.     /**
  15.      * @var NotificationManager
  16.      */
  17.     protected $notificationManager;
  18.     /**
  19.      * @var LoggerInterface
  20.      */
  21.     protected $logger;
  22.     public function __construct(
  23.         NotificationManager $notificationManager,
  24.         LoggerInterface $logger
  25.     ) {
  26.         $this->notificationManager $notificationManager;
  27.         $this->logger              $logger;
  28.     }
  29.     /**
  30.      * @return array
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             static::COMMENT_POSTED_DONE => 'onCommentPostedDone',
  36.         ];
  37.     }
  38.     /**
  39.      * Bootstraps status change hooks.
  40.      *
  41.      * @param NewCommentEvent $event
  42.      *
  43.      * @return void
  44.      */
  45.     public function onCommentPostedDone(NewCommentEvent $event): void
  46.     {
  47.         $request     $event->getSubject();
  48.         $commentRole $request->getCurrentStatus()->getCommentRole();
  49.         if (empty($commentRole)) {
  50.             $notification $this->notificationManager->getNewCommentNotification($request);
  51.             $this->notificationManager->notify($notification);
  52.         }
  53.         // if the user isn't from Suez organisation, a notification should be sent to Suez to let them know the action took place
  54.         $userRoles $event->getArgument('userRoles');
  55.         if (!in_array(UserRightsManagerInterface::ROLE_SUEZ$userRoles)) {
  56.             $this->notifySuezNewStatus($event);
  57.         }
  58.     }
  59.     public function notifySuezNewStatus(NewCommentEvent $event): void
  60.     {
  61.         $request      $event->getSubject();
  62.         $notification $this->notificationManager->getNewCommentNotification($request);
  63.         $this->logger->info('New comment from client user. Pushing notification to Asteo');
  64.         // Original email will be replaced by the consumer dedicated to pushing notifications to Suez.
  65.         // and we push
  66.         $this->notificationManager->notifyPce($notification);
  67.     }
  68. }