src/Modules/Notification/Event/Subscriber/SendFormSentNotification.php line 77

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\Entity\Form\RentPlane;
  5. use App\Modules\Contract\Entity\Form\RentYacht;
  6. use App\Modules\Contract\Event\ContractCreated;
  7. use App\Modules\Notification\Entity\NotificationEntity;
  8. use App\Modules\Notification\Message\System\FormCreatedNotification;
  9. use App\Modules\Notification\Service\NotificationService;
  10. use App\Modules\Contract\Service\ParticipantDetailsService;
  11. use App\Modules\TangibleAsset\Service\AssetDetailsService;
  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 SendFormSentNotification 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.     /**
  29.      * @var AssetDetailsService
  30.      */
  31.     private $assetDetailsService;
  32.     public function __construct(
  33.         NotificationService $notificationService,
  34.         ParticipantDetailsService $participantDetailsService,
  35.         RepresentativesService $representativesService,
  36.         AssetDetailsService $assetDetailsService,
  37.         LoggerInterface $logger
  38.     )
  39.     {
  40.         $this->notificationService $notificationService;
  41.         $this->logger $logger;
  42.         $this->participantDetailsService $participantDetailsService;
  43.         $this->representativesService $representativesService;
  44.         $this->assetDetailsService $assetDetailsService;
  45.     }
  46.     /**
  47.      * Returns an array of event names this subscriber wants to listen to.
  48.      *
  49.      * The array keys are event names and the value can be:
  50.      *
  51.      *  * The method name to call (priority defaults to 0)
  52.      *  * An array composed of the method name to call and the priority
  53.      *  * An array of arrays composed of the method names to call and respective
  54.      *    priorities, or 0 if unset
  55.      *
  56.      * For instance:
  57.      *
  58.      *  * ['eventName' => 'methodName']
  59.      *  * ['eventName' => ['methodName', $priority]]
  60.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  61.      *
  62.      * @return array The event names to listen to
  63.      */
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             ContractCreated::class => ['sendNotification']
  68.         ];
  69.     }
  70.     public function sendNotification(ContractCreated $event)
  71.     {
  72.         try {
  73.             $assetTitle $assetType null;
  74.             $contract $event->getContract();
  75.             $form $contract->getForm();
  76.             $asset $contract->getAsset();
  77.             $service $contract->getService();
  78.             $initiator $contract->getInitiator();
  79.             $executor $contract->getExecutor();
  80.             $sender $this->participantDetailsService->get($initiator->getId(), $initiator->getType());
  81.             if (!is_null($asset)) {
  82.                 $assetDetails $this->assetDetailsService->get($asset->getId(), $asset->getType());
  83.                 $assetTitle  $assetDetails->getTitle();
  84.             } else {
  85.                 $assetType $this->getAssetTypeBasedOnFormType($form->getType());
  86.             }
  87.             is_null($sender) ? $senderName '' $senderName $sender->getName();
  88.             $serviceName $service->getTitle();
  89.             $link = [
  90.                 'id' => $form->getId(),
  91.                 'entity' => NotificationEntity::CONTRACT_FORM,
  92.                 'form_type' => $form->getType()
  93.             ];
  94.             $receivers $this->representativesService->get($executor->getId(), $executor->getType());
  95.             $notification = new FormCreatedNotification($assetTitle$assetType$serviceName$link$senderName$receivers);
  96.             $this->notificationService->sendSystem($notification);
  97.         } catch (\Exception $e) {
  98.             $this->logger->error($e->getMessage());
  99.         }
  100.     }
  101.     private function getAssetTypeBasedOnFormType(string $formType)
  102.     {
  103.         switch ($formType) {
  104.             case RentPlane::TYPE:
  105.                 return 'plane';
  106.             case RentYacht::TYPE:
  107.                 return 'yacht';
  108.             default:
  109.                 return 'asset';
  110.         }
  111.     }
  112. }