src/App/Subscriber/ProposalEmailSubscriber.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use App\Email\EmailStrategy;
  4. use App\Email\ProposalCanceled;
  5. use App\Email\ProposalDealDownloadable;
  6. use App\Email\ProposalFunded;
  7. use App\Email\ProposalFundedInternalNotification;
  8. use App\Entity\Deal;
  9. use App\Entity\Project;
  10. use App\Entity\Proposal;
  11. use App\Entity\User;
  12. use App\Event\ProposalCanceledEvent;
  13. use App\Event\ProposalFundedEvent;
  14. use App\RabbitMq\Processing\PopProcessingEvent;
  15. use Sindrive\EmailSenderBundle\Service\EmailSender;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  18. use UploadsBundle\RabbitMq\Handler\FileHandlerFactoryHandler;
  19. class ProposalEmailSubscriber implements EventSubscriberInterface
  20. {
  21.     private EmailSender $emailSender;
  22.     private EmailStrategy $emailStrategy;
  23.     public function __construct(EmailSender $emailSenderEmailStrategy $emailStrategy)
  24.     {
  25.         $this->emailSender $emailSender;
  26.         $this->emailStrategy $emailStrategy;
  27.     }
  28.     public function onFunded(ProposalFundedEvent $event): void
  29.     {
  30.         $proposal $event->getProposal();
  31.         $this->sendEmailToBidders(
  32.             $proposal,
  33.             function (Proposal $proposalUser $bidUser): void {
  34.                 if (!$proposal instanceof Deal && !$proposal instanceof Project) {
  35.                     throw new UnexpectedTypeException($proposalDeal::class . '|' Project::class);
  36.                 }
  37.                 $this->emailSender->sendType(new ProposalFunded($proposal$bidUser$this->emailStrategy));
  38.             }
  39.         );
  40.         $this->emailSender->sendType(new ProposalFundedInternalNotification($proposal$this->emailStrategy));
  41.     }
  42.     public function onCanceled(ProposalCanceledEvent $event): void
  43.     {
  44.         $this->sendEmailToBidders(
  45.             $event->getProposal(),
  46.             function (Proposal $proposalUser $bidUser): void {
  47.                 if (!$proposal instanceof Deal && !$proposal instanceof Project) {
  48.                     throw new UnexpectedTypeException($proposalDeal::class . '|' Project::class);
  49.                 }
  50.                 $this->emailSender->sendType(new ProposalCanceled($proposal$bidUser$this->emailStrategy));
  51.             }
  52.         );
  53.     }
  54.     public function onPop(PopProcessingEvent $event): void
  55.     {
  56.         if ($event->getService() !== FileHandlerFactoryHandler::HANDLER) {
  57.             return;
  58.         }
  59.         /** @var string[] $arguments */
  60.         $arguments $event->getArguments();
  61.         if (!isset($arguments[0]) || $arguments[0] !== 'project_video') {
  62.             return;
  63.         }
  64.         if ($event->getMethod() !== 'move' && $event->getMethod() !== 'create') {
  65.             return;
  66.         }
  67.         /** @var Project $proposal */
  68.         $proposal $event->getEntity();
  69.         if (!$proposal->isFunded() || !$proposal->isFinished() || $proposal->isDownloadableEmailSent()) {
  70.             return;
  71.         }
  72.         $proposal->setDownloadableEmailSent();
  73.         $this->sendEmailToBidders(
  74.             $proposal,
  75.             function (Project $proposalUser $bidUser): void {
  76.                 $this->emailSender->sendType(new ProposalDealDownloadable($proposal$bidUser$this->emailStrategy));
  77.             }
  78.         );
  79.     }
  80.     /**
  81.      * @param Project|Deal $proposal
  82.      */
  83.     private function sendEmailToBidders(Proposal $proposal, callable $sendEmailCallable): void
  84.     {
  85.         $bidders = [];
  86.         foreach ($proposal->getBids() as $proposalBid) {
  87.             $bidUser $proposalBid->getUser();
  88.             if (isset($bidders[$bidUser->getId()])) {
  89.                 continue;
  90.             }
  91.             if (!$proposalBid->isJustRefunded() && ($proposalBid->isRefunded() || $proposalBid->isCanceled())) {
  92.                 continue;
  93.             }
  94.             $bidders[$bidUser->getId()] = true;
  95.             if (!$bidUser->shouldNotifyProposal($proposal)) {
  96.                 continue;
  97.             }
  98.             $sendEmailCallable($proposal$bidUser);
  99.         }
  100.     }
  101.     /**
  102.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  103.      */
  104.     public static function getSubscribedEvents(): array
  105.     {
  106.         return [
  107.             ProposalFundedEvent::class => 'onFunded',
  108.             ProposalCanceledEvent::class => 'onCanceled',
  109.             PopProcessingEvent::class => 'onPop',
  110.         ];
  111.     }
  112. }