<?php
namespace App\Modules\Notification\Event\Subscriber;
use App\Modules\ChangesRequest\Event\ChangesRequestCreated;
use App\Modules\ChangesRequest\Factory\CommandFactory;
use App\Modules\Notification\Entity\NotificationEntity;
use App\Modules\Notification\Message\System\ChangesRequest\PartnerWantsAddNewAddressNotification;
use App\Modules\Notification\Message\System\ChangesRequest\PartnerWantsAddServiceNotification;
use App\Modules\Notification\Message\System\ChangesRequest\PartnerWantsChangeAddressNotification;
use App\Modules\Notification\Message\System\ChangesRequest\UserWantsChangePasswordNotification;
use App\Modules\Notification\Service\NotificationService;
use App\Modules\User\Entity\User;
use App\Modules\User\Repository\UserRepositoryInterface;
use App\Modules\User\Service\PartnerService;
use App\Service\WebRoutes;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendChangesRequestCreatedNotification implements EventSubscriberInterface
{
private $notificationService;
private $logger;
/**
* @var UserRepositoryInterface
*/
private $userRepository;
/**
* @var PartnerService
*/
private $partnerService;
public function __construct(
NotificationService $notificationService,
UserRepositoryInterface $userRepository,
PartnerService $partnerService,
LoggerInterface $logger
)
{
$this->notificationService = $notificationService;
$this->logger = $logger;
$this->userRepository = $userRepository;
$this->partnerService = $partnerService;
}
/**
* 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 [
ChangesRequestCreated::class => ['sendNotification']
];
}
public function sendNotification(ChangesRequestCreated $event)
{
try {
$changesRequest = $event->getChangesRequest();
$initiator = $changesRequest->getInitiator();
$link = [
'id' => $changesRequest->getInitiator()->getId(),
'entity' => NotificationEntity::CHANGE_REQUEST
];
$recipients = $this->userRepository->findByRoles([User::ROLE_ADMIN]);
$notification = $this->createNotification($changesRequest->getOperation(), $initiator, $link, $recipients);
$this->notificationService->sendSystem($notification);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
private function createNotification(string $operation, User $initiator, array $link, $receivers)
{
switch ($operation) {
case CommandFactory::ADD_PARTNER_ADDRESS:
return $this->partnerWantsAddAddressNotification($initiator, $link, $receivers);
case CommandFactory::EDIT_PARTNER_ADDRESS:
return $this->partnerWantsChangeAddressNotification($initiator, $link, $receivers);
case CommandFactory::ADD_PARTNER_SERVICE:
return $this->partnerWantsAddServiceNotification($initiator, $link, $receivers);
case CommandFactory::CHANGE_PASSWORD:
return $this->userWantsChangePassword($initiator, $link, $receivers);
default:
throw new \Exception('Unknown operation '. $operation);
}
}
private function partnerWantsAddAddressNotification(User $initiator, array $link, $receivers)
{
$partner = $initiator->getPartner();
$partnerTitle = $partner->getCompanyName();
return new PartnerWantsAddNewAddressNotification($partnerTitle, $link, $initiator, $receivers);
}
private function partnerWantsChangeAddressNotification(User $initiator, array $link, $receivers)
{
$partner = $initiator->getPartner();
$partnerTitle = $partner->getCompanyName();
return new PartnerWantsChangeAddressNotification($partnerTitle, $link, $initiator, $receivers);
}
private function partnerWantsAddServiceNotification(User $initiator, array $link, $receivers)
{
$partner = $initiator->getPartner();
$partnerTitle = $partner->getCompanyName();
return new PartnerWantsAddServiceNotification($partnerTitle, $link, $initiator, $receivers);
}
private function userWantsChangePassword(User $initiator, array $link, $receivers)
{
return new UserWantsChangePasswordNotification($link, $initiator, $receivers);
}
}