<?php
namespace App\Modules\Notification\Event\Subscriber;
use App\Modules\Contract\Event\WarrantyFormUpdated;
use App\Modules\Contract\StateMachine\WarrantyFormState;
use App\Modules\Notification\Entity\NotificationEntity;
use App\Modules\Notification\Message\System\WarrantyFormStatusChangedNotification;
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 Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendWarrantyFormStatusChangedNotification 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 [
WarrantyFormUpdated::class => ['sendNotification']
];
}
public function sendNotification(WarrantyFormUpdated $event)
{
try {
$form = $event->getForm();
$contract = $form->getContract();
$initiator = $contract->getInitiator();
$executor = $contract->getExecutor();
$refNumber = $contract->getRef();
$link = [
'id' => $form->getId(),
'entity' => NotificationEntity::CONTRACT_FORM,
'form_type' => $form->getType()
];
if (in_array($form->getState(), WarrantyFormState::$initiatorAllowedActions)) {
$sender = $this->participantDetailsService->get($initiator->getId(), $initiator->getType());
$receivers = $this->representativesService->get($executor->getId(), $executor->getType());
} else {
$sender = $this->participantDetailsService->get($executor->getId(), $executor->getType());
$receivers = $this->representativesService->get($initiator->getId(), $initiator->getType());
}
$notification = new WarrantyFormStatusChangedNotification(
$refNumber,
$form->getState(),
$link,
$sender->getName(),
$receivers
);
$this->notificationService->sendSystem($notification);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
}