<?php
namespace App\Modules\Notification\Event\Subscriber;
use App\Modules\Contract\Entity\Form\Form;
use App\Modules\Contract\Entity\Form\RentPlane;
use App\Modules\Contract\Entity\Form\RentYacht;
use App\Modules\Contract\Event\ContractCreated;
use App\Modules\Notification\Entity\NotificationEntity;
use App\Modules\Notification\Message\System\FormCreatedNotification;
use App\Modules\Notification\Service\NotificationService;
use App\Modules\Contract\Service\ParticipantDetailsService;
use App\Modules\TangibleAsset\Service\AssetDetailsService;
use App\Modules\User\Service\RepresentativesService;
use App\Service\WebRoutes;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendFormSentNotification implements EventSubscriberInterface
{
private $notificationService;
private $logger;
/**
* @var ParticipantDetailsService
*/
private $participantDetailsService;
/**
* @var RepresentativesService
*/
private $representativesService;
/**
* @var AssetDetailsService
*/
private $assetDetailsService;
public function __construct(
NotificationService $notificationService,
ParticipantDetailsService $participantDetailsService,
RepresentativesService $representativesService,
AssetDetailsService $assetDetailsService,
LoggerInterface $logger
)
{
$this->notificationService = $notificationService;
$this->logger = $logger;
$this->participantDetailsService = $participantDetailsService;
$this->representativesService = $representativesService;
$this->assetDetailsService = $assetDetailsService;
}
/**
* 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 [
ContractCreated::class => ['sendNotification']
];
}
public function sendNotification(ContractCreated $event)
{
try {
$assetTitle = $assetType = null;
$contract = $event->getContract();
$form = $contract->getForm();
$asset = $contract->getAsset();
$service = $contract->getService();
$initiator = $contract->getInitiator();
$executor = $contract->getExecutor();
$sender = $this->participantDetailsService->get($initiator->getId(), $initiator->getType());
if (!is_null($asset)) {
$assetDetails = $this->assetDetailsService->get($asset->getId(), $asset->getType());
$assetTitle = $assetDetails->getTitle();
} else {
$assetType = $this->getAssetTypeBasedOnFormType($form->getType());
}
is_null($sender) ? $senderName = '' : $senderName = $sender->getName();
$serviceName = $service->getTitle();
$link = [
'id' => $form->getId(),
'entity' => NotificationEntity::CONTRACT_FORM,
'form_type' => $form->getType()
];
$receivers = $this->representativesService->get($executor->getId(), $executor->getType());
$notification = new FormCreatedNotification($assetTitle, $assetType, $serviceName, $link, $senderName, $receivers);
$this->notificationService->sendSystem($notification);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
private function getAssetTypeBasedOnFormType(string $formType)
{
switch ($formType) {
case RentPlane::TYPE:
return 'plane';
case RentYacht::TYPE:
return 'yacht';
default:
return 'asset';
}
}
}