src/Modules/Notification/Event/Subscriber/SendChangesRequestDeclinedNotification.php line 79

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