src/Modules/Notification/Event/Subscriber/SendFormAcceptedNotification.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Notification\Event\Subscriber;
  3. use App\Modules\Contract\Event\ContractCreated;
  4. use App\Modules\Contract\Event\FormAccepted;
  5. use App\Modules\Notification\Entity\NotificationEntity;
  6. use App\Modules\Notification\Message\System\FormCreatedNotification;
  7. use App\Modules\Notification\Message\System\FormRequestAcceptedNotification;
  8. use App\Modules\Notification\Service\NotificationService;
  9. use App\Modules\Contract\Service\ParticipantDetailsService;
  10. use App\Modules\User\Service\RepresentativesService;
  11. use App\Service\WebRoutes;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class SendFormAcceptedNotification implements EventSubscriberInterface
  15. {
  16.     private $notificationService;
  17.     private $logger;
  18.     /**
  19.      * @var ParticipantDetailsService
  20.      */
  21.     private $participantDetailsService;
  22.     /**
  23.      * @var RepresentativesService
  24.      */
  25.     private $representativesService;
  26.     public function __construct(
  27.         NotificationService $notificationService,
  28.         ParticipantDetailsService $participantDetailsService,
  29.         RepresentativesService $representativesService,
  30.         LoggerInterface $logger
  31.     )
  32.     {
  33.         $this->notificationService $notificationService;
  34.         $this->logger $logger;
  35.         $this->participantDetailsService $participantDetailsService;
  36.         $this->representativesService $representativesService;
  37.     }
  38.     /**
  39.      * Returns an array of event names this subscriber wants to listen to.
  40.      *
  41.      * The array keys are event names and the value can be:
  42.      *
  43.      *  * The method name to call (priority defaults to 0)
  44.      *  * An array composed of the method name to call and the priority
  45.      *  * An array of arrays composed of the method names to call and respective
  46.      *    priorities, or 0 if unset
  47.      *
  48.      * For instance:
  49.      *
  50.      *  * ['eventName' => 'methodName']
  51.      *  * ['eventName' => ['methodName', $priority]]
  52.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  53.      *
  54.      * @return array The event names to listen to
  55.      */
  56.     public static function getSubscribedEvents()
  57.     {
  58.         return [
  59.             FormAccepted::class => ['sendNotification']
  60.         ];
  61.     }
  62.     public function sendNotification(FormAccepted $event)
  63.     {
  64.         try {
  65.             $form $event->getForm();
  66.             if (!$form->acceptedByExecutor()) {
  67.                 return;
  68.             }
  69.             $contract $form->getContract();
  70.             $service $contract->getService();
  71.             $initiator $contract->getInitiator();
  72.             $executor $contract->getExecutor();
  73.             $sender $this->participantDetailsService->get($executor->getId(), $executor->getType());
  74.             is_null($sender) ? $senderName '' $senderName $sender->getName();
  75.             $refNumber  $contract->getRef();
  76.             $serviceName $service->getTitle();
  77.             $link = ['entity' => NotificationEntity::TCCS_MANAGEMENT];
  78.             $receivers $this->representativesService->get($initiator->getId(), $initiator->getType());
  79.             $notification = new FormRequestAcceptedNotification($refNumber$serviceName$link$senderName$receivers);
  80.             $this->notificationService->sendSystem($notification);
  81.         } catch (\Exception $e) {
  82.             $this->logger->error($e->getMessage());
  83.         }
  84.     }
  85. }