<?php
namespace App\Modules\Notification\Event\Subscriber;
use App\Modules\Contract\Entity\Form\Form;
use App\Modules\Contract\Event\FormDeclined;
use App\Modules\Contract\ValueObject\Participant;
use App\Modules\Notification\Entity\NotificationEntity;
use App\Modules\Notification\Message\System\FormRequestDeclinedNotification;
use App\Modules\Notification\Message\System\FormRequestDeclinedValueNotification;
use App\Modules\Notification\Message\System\FormRequestInitiatorDeclinedValueNotification;
use App\Modules\Notification\Service\NotificationService;
use App\Modules\Contract\Service\ParticipantDetailsService;
use App\Modules\User\Service\RepresentativesService;
use App\Service\WebRoutes;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendFormDeclinedNotification implements EventSubscriberInterface
{
private $notificationService;
private $logger;
/**
* @var ParticipantDetailsService
*/
private $participantDetailsService;
/**
* @var RepresentativesService
*/
private $representativesService;
public function __construct(
NotificationService $notificationService,
ParticipantDetailsService $participantDetailsService,
RepresentativesService $representativesService,
LoggerInterface $logger
)
{
$this->notificationService = $notificationService;
$this->logger = $logger;
$this->participantDetailsService = $participantDetailsService;
$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 [
FormDeclined::class => ['sendNotification']
];
}
public function sendNotification(FormDeclined $event)
{
try {
$form = $event->getForm();
$contract = $form->getContract();
$initiator = $contract->getInitiator();
$executor = $contract->getExecutor();
$refNumber = $contract->getRef();
if ($form->isDeclinedValueByInitiator()) {
$this->sendDeclinedValueByInitiatorNotification($initiator, $executor, $refNumber, $form);
} else {
$this->sendDeclinedValueByExecutorNotification($initiator, $executor, $refNumber);
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
private function sendDeclinedValueByExecutorNotification(Participant $initiator, Participant $executor, string $refNumber)
{
$sender = $this->participantDetailsService->get($executor->getId(), $executor->getType());
$senderName = !is_null($sender) ? $sender->getName() : '';
$receivers = $this->representativesService->get($initiator->getId(), $initiator->getType());
$link = ['entity' => NotificationEntity::TCCS_MANAGEMENT];
$notification = new FormRequestDeclinedNotification($refNumber, $link, $senderName, $receivers);
$this->notificationService->sendSystem($notification);
}
private function sendDeclinedValueByInitiatorNotification(Participant $initiator, Participant $executor, string $refNumber, Form $form)
{
$sender = $this->participantDetailsService->get($initiator->getId(), $initiator->getType());
$senderName = !is_null($sender) ? $sender->getName() : '';
$receivers = $this->representativesService->get($executor->getId(), $executor->getType());
$link = [
'id' => $form->getId(),
'entity' => NotificationEntity::CONTRACT_FORM,
'form_type' => $form->getType()
];
$notification = new FormRequestInitiatorDeclinedValueNotification($refNumber, $link, $senderName, $receivers);
$this->notificationService->sendSystem($notification);
}
}