<?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\AssetOnHoldNotification;
use App\Modules\Notification\Service\NotificationService;
use App\Modules\TangibleAsset\Event\AssetOnHold;
use App\Modules\TangibleAsset\Service\AssetOwnerRepresentativesService;
use App\Service\WebRoutes;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendPlaneOnHoldNotification implements EventSubscriberInterface
{
private $notificationService;
private $logger;
private $representativesService;
public function __construct(
NotificationService $notificationService,
AssetOwnerRepresentativesService $representativesService,
LoggerInterface $logger
)
{
$this->notificationService = $notificationService;
$this->logger = $logger;
$this->representativesService = $representativesService;
}
/**
* 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 [
AssetOnHold::class => ['sendNotification']
];
}
public function sendNotification(AssetOnHold $event)
{
try {
$asset = $event->getAsset();
$assetOwner = $asset->getOwnerID();
$assetTitle = $asset->getTitle();
$assetType = Asset::getTypeTitle($asset->getType());
$assetLink = [
'entity' => NotificationEntity::$assetEntities[$asset->getType()],
'id' => $asset->getId()
];
$receivers = $this->representativesService->get($assetOwner->getId(), $assetOwner->getOwnerType());
$notification = new AssetOnHoldNotification($assetTitle, $assetType, $assetLink, $receivers);
$this->notificationService->sendSystem($notification);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
}