src/Modules/Notification/Event/Subscriber/SendWarrantyFormStatusChangedNotification.php line 74

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