<?php
namespace App\Modules\Notification\Event\Subscriber;
use App\Modules\ChangesRequest\Entity\ChangesRequest;
use App\Modules\ChangesRequest\Event\ChangesRequestApproved;
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\AddNewPartnerAddressApprovedNotification;
use App\Modules\Notification\Message\System\ChangesRequest\AddNewPartnerServiceApprovedNotification;
use App\Modules\Notification\Message\System\ChangesRequest\ChangePartnerAddressApprovedNotification;
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 Doctrine\Common\Collections\ArrayCollection;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendChangesRequestApprovedNotification 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 [
ChangesRequestApproved::class => ['sendNotification']
];
}
public function sendNotification(ChangesRequestApproved $event)
{
try {
$changesRequest = $event->getChangesRequest();
$recipients = new ArrayCollection();
$recipients->add($changesRequest->getInitiator());
$notification = $this->createNotification($changesRequest, $recipients);
$this->notificationService->sendSystem($notification);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
private function createNotification(ChangesRequest $changesRequest, $receivers)
{
$partnerId = $changesRequest->getEntity()->getId();
$link = [
'entity' => NotificationEntity::PARTNER,
'id' => $partnerId
];
switch ($changesRequest->getOperation()) {
case CommandFactory::ADD_PARTNER_ADDRESS:
return new AddNewPartnerAddressApprovedNotification($link, $receivers);
case CommandFactory::EDIT_PARTNER_ADDRESS:
return new ChangePartnerAddressApprovedNotification($link, $receivers);
case CommandFactory::ADD_PARTNER_SERVICE:
return new AddNewPartnerServiceApprovedNotification($link, $receivers);
default:
throw new \Exception('Unknown operation '. $changesRequest->getOperation());
}
}
}