src/Modules/Notification/Event/Subscriber/SendListingOrAssetCreatedNotification.php line 109

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\AssetOrListingSentOnModerationNotification;
  6. use App\Modules\Notification\Service\NotificationService;
  7. use App\Modules\TangibleAsset\Event\AssetSentOnModeration;
  8. use App\Modules\TangibleAsset\Event\ListingCreated;
  9. use App\Modules\TangibleAsset\Event\ListingSentOnModeration;
  10. use App\Modules\TangibleAsset\Service\AssetOwnerDetailsService;
  11. use App\Modules\User\Entity\User;
  12. use App\Modules\User\Repository\UserRepositoryInterface;
  13. use App\Service\WebRoutes;
  14. use Psr\Log\LoggerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. class SendListingOrAssetCreatedNotification implements EventSubscriberInterface
  18. {
  19.     private $notificationService;
  20.     private $logger;
  21.     private $assetOwnerDetailsService;
  22.     /**
  23.      * @var UserRepositoryInterface
  24.      */
  25.     private $userRepository;
  26.     /**
  27.      * @var TokenStorageInterface
  28.      */
  29.     private $tokenStorage;
  30.     public function __construct(
  31.         NotificationService $notificationService,
  32.         AssetOwnerDetailsService $assetOwnerDetailsService,
  33.         UserRepositoryInterface $userRepository,
  34.         TokenStorageInterface $tokenStorage,
  35.         LoggerInterface $logger
  36.     )
  37.     {
  38.         $this->notificationService $notificationService;
  39.         $this->logger $logger;
  40.         $this->assetOwnerDetailsService $assetOwnerDetailsService;
  41.         $this->userRepository $userRepository;
  42.         $this->tokenStorage $tokenStorage;
  43.     }
  44.     /**
  45.      * Returns an array of event names this subscriber wants to listen to.
  46.      *
  47.      * The array keys are event names and the value can be:
  48.      *
  49.      *  * The method name to call (priority defaults to 0)
  50.      *  * An array composed of the method name to call and the priority
  51.      *  * An array of arrays composed of the method names to call and respective
  52.      *    priorities, or 0 if unset
  53.      *
  54.      * For instance:
  55.      *
  56.      *  * ['eventName' => 'methodName']
  57.      *  * ['eventName' => ['methodName', $priority]]
  58.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  59.      *
  60.      * @return array The event names to listen to
  61.      */
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             ListingSentOnModeration::class => ['sendListingSentOnModerationNotification'],
  66.             AssetSentOnModeration::class => ['sendAssetSentOnModerationNotification'],
  67.         ];
  68.     }
  69.     public function sendListingSentOnModerationNotification(ListingSentOnModeration $event)
  70.     {
  71.         try {
  72.             $listing $event->getListing();
  73.             $listingOwner $listing->getOwnerID();
  74.             $listingOwnerDetails $this->assetOwnerDetailsService->get($listingOwner->getId(), $listingOwner->getOwnerType());
  75.             $listingTypeTitle Asset::getTypeTitle($listing->getType());
  76.             $listingTitle Asset::getTypeTitle($listing->getType());
  77.             $listingLink = [
  78.                 'entity' => NotificationEntity::$assetEntities[$listing->getType()],
  79.                 'id' => $listing->getId()
  80.             ];
  81.             $listingOwnerName $listingOwnerDetails->getName();
  82.             $recipients $this->userRepository->findByRoles([User::ROLE_ADMIN]);
  83.             $notification = new AssetOrListingSentOnModerationNotification(
  84.                 $this->getInitiator(),
  85.                 $listingTitle,
  86.                 $listingOwnerName,
  87.                 $listingTypeTitle,
  88.                 $listingLink,
  89.                 $recipients
  90.             );
  91.             $this->notificationService->sendSystem($notification);
  92.         } catch (\Exception $e) {
  93.             $this->logger->error($e->getMessage());
  94.         }
  95.     }
  96.     public function sendAssetSentOnModerationNotification(AssetSentOnModeration $event)
  97.     {
  98.         try {
  99.             $asset $event->getAsset();
  100.             $assetOwner $asset->getOwnerID();
  101.             $assetTitle $asset->getTitle();
  102.             $assetTypeTitle Asset::getTypeTitle($asset->getType());
  103.             $assetLink = [
  104.                 'entity' => NotificationEntity::$assetEntities[$asset->getType()],
  105.                 'id' => $asset->getId()
  106.             ];
  107.             $assetOwnerDetails $this->assetOwnerDetailsService->get($assetOwner->getId(), $assetOwner->getOwnerType());
  108.             $assetOwnerName $assetOwnerDetails->getName();
  109.             $recipients $this->userRepository->findByRoles([User::ROLE_ADMIN]);
  110.             $notification = new AssetOrListingSentOnModerationNotification(
  111.                 $this->getInitiator(),
  112.                 $assetTitle,
  113.                 $assetOwnerName,
  114.                 $assetTypeTitle,
  115.                 $assetLink,
  116.                 $recipients
  117.             );
  118.             $this->notificationService->sendSystem($notification);
  119.         } catch (\Exception $e) {
  120.             $this->logger->error($e->getMessage());
  121.         }
  122.     }
  123.     private function getInitiator()
  124.     {
  125.         $token $this->tokenStorage->getToken();
  126.         if (!is_null($token)) {
  127.             return $token->getUser();
  128.         }
  129.     }
  130. }