src/Listener/AttachmentPostedSubscriber.php line 61

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