src/Modules/Notification/Event/Subscriber/SendPlaneApprovedNotification.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Notification\Event\Subscriber;
  3. use App\Modules\Contract\ValueObject\Asset;
  4. use App\Modules\Notification\Entity\NotificationEntity;
  5. use App\Modules\Notification\Message\System\AssetApprovedNotification;
  6. use App\Modules\Notification\Service\NotificationService;
  7. use App\Modules\TangibleAsset\Event\AssetApproved;
  8. use App\Modules\TangibleAsset\Service\AssetOwnerRepresentativesService;
  9. use App\Service\WebRoutes;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class SendPlaneApprovedNotification implements EventSubscriberInterface
  13. {
  14.     private $notificationService;
  15.     private $logger;
  16.     private $representativesService;
  17.     public function __construct(
  18.         NotificationService $notificationService,
  19.         AssetOwnerRepresentativesService $representativesService,
  20.         LoggerInterface $logger
  21.     )
  22.     {
  23.         $this->notificationService $notificationService;
  24.         $this->logger $logger;
  25.         $this->representativesService $representativesService;
  26.     }
  27.     /**
  28.      * Returns an array of event names this subscriber wants to listen to.
  29.      *
  30.      * The array keys are event names and the value can be:
  31.      *
  32.      *  * The method name to call (priority defaults to 0)
  33.      *  * An array composed of the method name to call and the priority
  34.      *  * An array of arrays composed of the method names to call and respective
  35.      *    priorities, or 0 if unset
  36.      *
  37.      * For instance:
  38.      *
  39.      *  * ['eventName' => 'methodName']
  40.      *  * ['eventName' => ['methodName', $priority]]
  41.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  42.      *
  43.      * @return array The event names to listen to
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             AssetApproved::class => ['sendNotification']
  49.         ];
  50.     }
  51.     public function sendNotification(AssetApproved $event)
  52.     {
  53.         try {
  54.             $asset $event->getAsset();
  55.             $assetOwner $asset->getOwnerID();
  56.             $assetTypeTitle Asset::getTypeTitle($asset->getType());
  57.             $assetLink = [
  58.                 'entity' => NotificationEntity::$assetEntities[$asset->getType()],
  59.                 'id' => $asset->getId()
  60.             ];
  61.             $receivers $this->representativesService->get($assetOwner->getId(), $assetOwner->getOwnerType());
  62.             $notification = new AssetApprovedNotification($asset->getTitle(), $assetTypeTitle$assetLink$receivers);
  63.             $this->notificationService->sendSystem($notification);
  64.         } catch (\Exception $e) {
  65.             $this->logger->error($e->getMessage());
  66.         }
  67.     }
  68. }