src/Modules/Notification/Event/Subscriber/SendFormDeclinedNotification.php line 71

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