src/EventsSubscriber/Notify/NotifySubscriber.php line 131

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventsSubscriber\Notify;
  4. use App\Event\Activity\ActivityActivatedEvent;
  5. use App\Event\Activity\ActivityDeclinedEvent;
  6. use App\Event\Activity\ActivityBoostCodeReceived;
  7. use App\Event\Activity\ActivityBoostCodeRequested;
  8. use App\Event\Activity\ConfirmPayoutEvent;
  9. use App\Event\Activity\ReviewEvent;
  10. use App\Event\Campaign\ActivatedEvent;
  11. use App\Event\Campaign\CampaignBudgetIncreasedEvent;
  12. use App\Event\Campaign\CampaignPendingEvent;
  13. use App\Event\Campaign\CampaignPurchaseOrderUpdatedEvent;
  14. use App\Event\Campaign\RenewedCampaignEvent;
  15. use App\Event\Creator\CreatorReAuthRequiredEvent;
  16. use App\Event\Payment\PaymentProcessPOAddedEvent;
  17. use App\Messenger\Notification\NotificationNewCampaignMessage;
  18. use App\Services\Manager\Event\ManagerCreatedEvent;
  19. use App\Services\Sponsor\Event\SponsorCreatedEvent;
  20. use App\Services\User\Exception\NotifyException;
  21. use App\Services\User\NotifyService;
  22. use App\Services\User\Repository\CreatorRepository;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use Doctrine\ORM\NonUniqueResultException;
  25. use Doctrine\ORM\NoResultException;
  26. use Psr\Log\LoggerInterface;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\Mailer\Event\MessageEvent;
  29. use Symfony\Component\Messenger\MessageBusInterface;
  30. use Symfony\Component\Mime\Address;
  31. use Symfony\Component\Mime\Email;
  32. class NotifySubscriber implements EventSubscriberInterface
  33. {
  34.     private const CREATOR_STEP 25000;
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             MessageEvent::class => 'onMessage',
  39.             ActivatedEvent::class => 'onActivatedCampaign',
  40.             CampaignPendingEvent::class => 'onPendingCampaign',
  41.             ReviewEvent::class => 'onReview',
  42.             ConfirmPayoutEvent::class => 'onConfirmPayout',
  43.             SponsorCreatedEvent::class => 'onSponsorCreated',
  44.             ManagerCreatedEvent::class => 'onManagerCreated',
  45.             RenewedCampaignEvent::class => ['onRenewedCampaign'5],
  46.             ActivityActivatedEvent::class => 'onActivatedActivity',
  47.             ActivityDeclinedEvent::class => 'onDeclinedActivity',
  48.             ActivityBoostCodeRequested::class => 'onActivityBoostCodeRequested',
  49.             ActivityBoostCodeReceived::class => 'onActivityBoostCodeReceived',
  50.             CampaignBudgetIncreasedEvent::class => 'onCampaignBudgetIncreased',
  51.             CampaignPurchaseOrderUpdatedEvent::class => 'onCampaignPurchaseOrderUpdated',
  52.             PaymentProcessPOAddedEvent::class => 'onPaymentProcessPOAdded',
  53.             CreatorReAuthRequiredEvent::class => 'onCreatorReAuthRequired',
  54.         ];
  55.     }
  56.     public function __construct(
  57.         private readonly NotifyService $service,
  58.         private readonly LoggerInterface $notifyLogger,
  59.         private readonly MessageBusInterface $bus,
  60.         private readonly CreatorRepository $creatorRepository,
  61.         private readonly EntityManagerInterface $entityManager,
  62.         private readonly string $defaultFromEmail,
  63.     ) {
  64.     }
  65.     public function onMessage(MessageEvent $event): void
  66.     {
  67.         $message $event->getMessage();
  68.         if (!$message instanceof Email) {
  69.             return;
  70.         }
  71.         $from = [];
  72.         foreach ($message->getFrom() as $item) {
  73.             $from[] = new Address($item->getAddress(), 'SOUND.ME');
  74.         }
  75.         if ($from === []) {
  76.             $from[] = new Address($this->defaultFromEmail'SOUND.ME');
  77.         }
  78.         $message->from(...$from);
  79.     }
  80.     /**
  81.      * @throws NonUniqueResultException
  82.      * @throws NoResultException
  83.      */
  84.     public function onActivatedCampaign(ActivatedEvent $event): void
  85.     {
  86.         $this->notifySponsorAfterCampaignActivated($event);
  87.         $maxId $this->creatorRepository->getMaxId();
  88.         for ($i 0$i $maxId$i += self::CREATOR_STEP) {
  89.             $this->bus->dispatch(
  90.                 new NotificationNewCampaignMessage(
  91.                     $event->getCampaign()->getId(),
  92.                     $i,
  93.                     $i self::CREATOR_STEP
  94.                 )
  95.             );
  96.         }
  97.     }
  98.     public function onPendingCampaign(CampaignPendingEvent $event): void
  99.     {
  100.         try {
  101.             $this->service->sendCampaignPendingNotification($event->getCampaign());
  102.         } catch (NotifyException $exception) {
  103.             $this->notifyLogger->warning($exception);
  104.         }
  105.     }
  106.     public function onConfirmPayout(ConfirmPayoutEvent $event): void
  107.     {
  108.         try {
  109.             $this->service->confirmPayout($event->getActivity());
  110.         } catch (NotifyException $exception) {
  111.             $this->notifyLogger->warning($exception);
  112.         }
  113.     }
  114.     public function onReview(ReviewEvent $reviewEvent): void
  115.     {
  116.         try {
  117.             $this->service->review($reviewEvent->getActivity());
  118.         } catch (NotifyException $exception) {
  119.             $this->notifyLogger->warning($exception);
  120.         }
  121.     }
  122.     public function onSponsorCreated(SponsorCreatedEvent $sponsorCreatedEvent): void
  123.     {
  124.         if (!$sponsorCreatedEvent->needNotify()) {
  125.             return;
  126.         }
  127.         try {
  128.             $this->service->newSponsor($sponsorCreatedEvent->getSponsor(), $sponsorCreatedEvent->getPlainPassword());
  129.         } catch (NotifyException $exception) {
  130.             $this->notifyLogger->warning($exception);
  131.         }
  132.     }
  133.     public function onManagerCreated(ManagerCreatedEvent $managerCreatedEvent): void
  134.     {
  135.         if (!$managerCreatedEvent->needNotify()) {
  136.             return;
  137.         }
  138.         try {
  139.             $this->service->newManager($managerCreatedEvent->getManager(), $managerCreatedEvent->getPlainPassword());
  140.         } catch (NotifyException $exception) {
  141.             $this->notifyLogger->warning($exception);
  142.         }
  143.     }
  144.     public function onRenewedCampaign(RenewedCampaignEvent $event): void
  145.     {
  146.         $maxId $this->creatorRepository->getMaxId();
  147.         for ($i 0$i $maxId$i += self::CREATOR_STEP) {
  148.             $this->bus->dispatch(
  149.                 new NotificationNewCampaignMessage(
  150.                     $event->getCampaign()->getId(),
  151.                     $i,
  152.                     $i self::CREATOR_STEP
  153.                 )
  154.             );
  155.         }
  156.     }
  157.     public function onActivatedActivity(ActivityActivatedEvent $event): void
  158.     {
  159.         try {
  160.             $this->service->sendActivityActivatedSponsorNotification($event->getActivity());
  161.         } catch (NotifyException $exception) {
  162.             $this->notifyLogger->warning($exception);
  163.         }
  164.         try {
  165.             $this->service->videoApproved($event->getActivity());
  166.         } catch (NotifyException $exception) {
  167.             $this->notifyLogger->warning($exception);
  168.         }
  169.     }
  170.     public function onDeclinedActivity(ActivityDeclinedEvent $event): void
  171.     {
  172.         // Push is sent from NotifyService::declineByReason() (QC, statistics, async handlers).
  173.     }
  174.     private function notifySponsorAfterCampaignActivated(ActivatedEvent $activatedEvent): void
  175.     {
  176.         try {
  177.             $this->service->sendCampaignActivatedNotification($activatedEvent->getCampaign());
  178.         } catch (NotifyException $exception) {
  179.             $this->notifyLogger->warning($exception);
  180.         }
  181.     }
  182.     public function onActivityBoostCodeRequested(ActivityBoostCodeRequested $activityBoostCodeRequested): void
  183.     {
  184.         try {
  185.             $activity $activityBoostCodeRequested->getActivity();
  186.             $this->service->boostCodeRequest($activity);
  187.             if (!$activity->isBoostCodeRequestedByAdmin()) {
  188.                 $this->service->sendActivityBoostCodeRequestedSponsorNotification($activity);
  189.             }
  190.         } catch (NotifyException $exception) {
  191.             $this->notifyLogger->warning($exception);
  192.         }
  193.     }
  194.     public function onActivityBoostCodeReceived(ActivityBoostCodeReceived $activityBoostCodeReceived): void
  195.     {
  196.         try {
  197.             $activity $activityBoostCodeReceived->getActivity();
  198.             $this->entityManager->refresh($activity);
  199.             $this->service->sendActivityBoostCodeReceivedSponsorNotification(
  200.                 activity$activity
  201.             );
  202.         } catch (NotifyException $exception) {
  203.             $this->notifyLogger->warning($exception);
  204.         }
  205.     }
  206.     public function onCampaignBudgetIncreased(CampaignBudgetIncreasedEvent $campaignBudgetIncreasedEvent): void
  207.     {
  208.         try {
  209.             $this->service->sendCampaignBudgetIncreasedSponsorNotification(
  210.                 campaign$campaignBudgetIncreasedEvent->getCampaign(),
  211.                 amount$campaignBudgetIncreasedEvent->getAmount()
  212.             );
  213.         } catch (NotifyException $exception) {
  214.             $this->notifyLogger->warning($exception);
  215.         }
  216.     }
  217.     public function onCampaignPurchaseOrderUpdated(CampaignPurchaseOrderUpdatedEvent $campaignPurchaseOrderUpdatedEvent): void
  218.     {
  219.         try {
  220.             $this->service->sendCampaignPurchaseOrderUpdatedNotification(
  221.                 campaign$campaignPurchaseOrderUpdatedEvent->getCampaign()
  222.             );
  223.         } catch (NotifyException $exception) {
  224.             $this->notifyLogger->warning($exception);
  225.         }
  226.     }
  227.     public function onPaymentProcessPOAdded(PaymentProcessPOAddedEvent $paymentProcessPOAddedEvent): void
  228.     {
  229.         try {
  230.             $this->service->sendPaymentProcessPOAddedNotification(
  231.                 paymentProcess$paymentProcessPOAddedEvent->getPaymentProcess()
  232.             );
  233.         } catch (NotifyException $exception) {
  234.             $this->notifyLogger->warning($exception);
  235.         }
  236.     }
  237.     public function onCreatorReAuthRequired(CreatorReAuthRequiredEvent $event): void
  238.     {
  239.         try {
  240.             $this->service->creatorReAuthRequired($event->getCreator(), $event->getPlatform());
  241.         } catch (NotifyException $exception) {
  242.             $this->notifyLogger->warning($exception);
  243.         }
  244.     }
  245. }