<?php
namespace App\Modules\Notification\Event\Subscriber;
use App\Modules\Contract\ValueObject\Asset;
use App\Modules\Notification\Entity\NotificationEntity;
use App\Modules\Notification\Message\System\AssetOrListingSentOnModerationNotification;
use App\Modules\Notification\Service\NotificationService;
use App\Modules\TangibleAsset\Event\AssetSentOnModeration;
use App\Modules\TangibleAsset\Event\ListingCreated;
use App\Modules\TangibleAsset\Event\ListingSentOnModeration;
use App\Modules\TangibleAsset\Service\AssetOwnerDetailsService;
use App\Modules\User\Entity\User;
use App\Modules\User\Repository\UserRepositoryInterface;
use App\Service\WebRoutes;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class SendListingOrAssetCreatedNotification implements EventSubscriberInterface
{
private $notificationService;
private $logger;
private $assetOwnerDetailsService;
/**
* @var UserRepositoryInterface
*/
private $userRepository;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
public function __construct(
NotificationService $notificationService,
AssetOwnerDetailsService $assetOwnerDetailsService,
UserRepositoryInterface $userRepository,
TokenStorageInterface $tokenStorage,
LoggerInterface $logger
)
{
$this->notificationService = $notificationService;
$this->logger = $logger;
$this->assetOwnerDetailsService = $assetOwnerDetailsService;
$this->userRepository = $userRepository;
$this->tokenStorage = $tokenStorage;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
ListingSentOnModeration::class => ['sendListingSentOnModerationNotification'],
AssetSentOnModeration::class => ['sendAssetSentOnModerationNotification'],
];
}
public function sendListingSentOnModerationNotification(ListingSentOnModeration $event)
{
try {
$listing = $event->getListing();
$listingOwner = $listing->getOwnerID();
$listingOwnerDetails = $this->assetOwnerDetailsService->get($listingOwner->getId(), $listingOwner->getOwnerType());
$listingTypeTitle = Asset::getTypeTitle($listing->getType());
$listingTitle = Asset::getTypeTitle($listing->getType());
$listingLink = [
'entity' => NotificationEntity::$assetEntities[$listing->getType()],
'id' => $listing->getId()
];
$listingOwnerName = $listingOwnerDetails->getName();
$recipients = $this->userRepository->findByRoles([User::ROLE_ADMIN]);
$notification = new AssetOrListingSentOnModerationNotification(
$this->getInitiator(),
$listingTitle,
$listingOwnerName,
$listingTypeTitle,
$listingLink,
$recipients
);
$this->notificationService->sendSystem($notification);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
public function sendAssetSentOnModerationNotification(AssetSentOnModeration $event)
{
try {
$asset = $event->getAsset();
$assetOwner = $asset->getOwnerID();
$assetTitle = $asset->getTitle();
$assetTypeTitle = Asset::getTypeTitle($asset->getType());
$assetLink = [
'entity' => NotificationEntity::$assetEntities[$asset->getType()],
'id' => $asset->getId()
];
$assetOwnerDetails = $this->assetOwnerDetailsService->get($assetOwner->getId(), $assetOwner->getOwnerType());
$assetOwnerName = $assetOwnerDetails->getName();
$recipients = $this->userRepository->findByRoles([User::ROLE_ADMIN]);
$notification = new AssetOrListingSentOnModerationNotification(
$this->getInitiator(),
$assetTitle,
$assetOwnerName,
$assetTypeTitle,
$assetLink,
$recipients
);
$this->notificationService->sendSystem($notification);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
private function getInitiator()
{
$token = $this->tokenStorage->getToken();
if (!is_null($token)) {
return $token->getUser();
}
}
}