src/Modules/Notification/Event/Subscriber/SendChangesRequestApprovedNotification.php line 75

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