src/Modules/Notification/Event/Subscriber/SendChangesRequestCreatedNotification.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Notification\Event\Subscriber;
  3. use App\Modules\ChangesRequest\Event\ChangesRequestCreated;
  4. use App\Modules\ChangesRequest\Factory\CommandFactory;
  5. use App\Modules\Notification\Entity\NotificationEntity;
  6. use App\Modules\Notification\Message\System\ChangesRequest\PartnerWantsAddNewAddressNotification;
  7. use App\Modules\Notification\Message\System\ChangesRequest\PartnerWantsAddServiceNotification;
  8. use App\Modules\Notification\Message\System\ChangesRequest\PartnerWantsChangeAddressNotification;
  9. use App\Modules\Notification\Message\System\ChangesRequest\UserWantsChangePasswordNotification;
  10. use App\Modules\Notification\Service\NotificationService;
  11. use App\Modules\User\Entity\User;
  12. use App\Modules\User\Repository\UserRepositoryInterface;
  13. use App\Modules\User\Service\PartnerService;
  14. use App\Service\WebRoutes;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class SendChangesRequestCreatedNotification implements EventSubscriberInterface
  18. {
  19.     private $notificationService;
  20.     private $logger;
  21.     /**
  22.      * @var UserRepositoryInterface
  23.      */
  24.     private $userRepository;
  25.     /**
  26.      * @var PartnerService
  27.      */
  28.     private $partnerService;
  29.     public function __construct(
  30.         NotificationService $notificationService,
  31.         UserRepositoryInterface $userRepository,
  32.         PartnerService $partnerService,
  33.         LoggerInterface $logger
  34.     )
  35.     {
  36.         $this->notificationService $notificationService;
  37.         $this->logger $logger;
  38.         $this->userRepository $userRepository;
  39.         $this->partnerService $partnerService;
  40.     }
  41.     /**
  42.      * Returns an array of event names this subscriber wants to listen to.
  43.      *
  44.      * The array keys are event names and the value can be:
  45.      *
  46.      *  * The method name to call (priority defaults to 0)
  47.      *  * An array composed of the method name to call and the priority
  48.      *  * An array of arrays composed of the method names to call and respective
  49.      *    priorities, or 0 if unset
  50.      *
  51.      * For instance:
  52.      *
  53.      *  * ['eventName' => 'methodName']
  54.      *  * ['eventName' => ['methodName', $priority]]
  55.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  56.      *
  57.      * @return array The event names to listen to
  58.      */
  59.     public static function getSubscribedEvents()
  60.     {
  61.         return [
  62.             ChangesRequestCreated::class => ['sendNotification']
  63.         ];
  64.     }
  65.     public function sendNotification(ChangesRequestCreated $event)
  66.     {
  67.         try {
  68.             $changesRequest $event->getChangesRequest();
  69.             $initiator $changesRequest->getInitiator();
  70.             $link = [
  71.                 'id' => $changesRequest->getInitiator()->getId(),
  72.                 'entity' => NotificationEntity::CHANGE_REQUEST
  73.             ];
  74.             $recipients $this->userRepository->findByRoles([User::ROLE_ADMIN]);
  75.             $notification $this->createNotification($changesRequest->getOperation(), $initiator$link$recipients);
  76.             $this->notificationService->sendSystem($notification);
  77.         } catch (\Exception $e) {
  78.             $this->logger->error($e->getMessage());
  79.         }
  80.     }
  81.     private function createNotification(string $operationUser $initiator, array $link$receivers)
  82.     {
  83.         switch ($operation) {
  84.             case CommandFactory::ADD_PARTNER_ADDRESS:
  85.                 return $this->partnerWantsAddAddressNotification($initiator$link$receivers);
  86.             case CommandFactory::EDIT_PARTNER_ADDRESS:
  87.                 return $this->partnerWantsChangeAddressNotification($initiator$link$receivers);
  88.             case CommandFactory::ADD_PARTNER_SERVICE:
  89.                 return $this->partnerWantsAddServiceNotification($initiator$link$receivers);
  90.             case CommandFactory::CHANGE_PASSWORD:
  91.                 return $this->userWantsChangePassword($initiator$link$receivers);
  92.             default:
  93.                 throw new \Exception('Unknown operation '$operation);
  94.         }
  95.     }
  96.     private function partnerWantsAddAddressNotification(User $initiator, array $link$receivers)
  97.     {
  98.         $partner $initiator->getPartner();
  99.         $partnerTitle $partner->getCompanyName();
  100.         return new PartnerWantsAddNewAddressNotification($partnerTitle$link$initiator$receivers);
  101.     }
  102.     private function partnerWantsChangeAddressNotification(User $initiator, array $link$receivers)
  103.     {
  104.         $partner $initiator->getPartner();
  105.         $partnerTitle $partner->getCompanyName();
  106.         return new PartnerWantsChangeAddressNotification($partnerTitle$link$initiator$receivers);
  107.     }
  108.     private function partnerWantsAddServiceNotification(User $initiator, array $link$receivers)
  109.     {
  110.         $partner $initiator->getPartner();
  111.         $partnerTitle $partner->getCompanyName();
  112.         return new PartnerWantsAddServiceNotification($partnerTitle$link$initiator$receivers);
  113.     }
  114.     private function userWantsChangePassword(User $initiator, array $link$receivers)
  115.     {
  116.         return new UserWantsChangePasswordNotification($link$initiator$receivers);
  117.     }
  118. }