src/App/Subscriber/ProposalNotificationSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use App\Doctrine\Common\Cache\TaggedMemcachedCache;
  4. use App\Entity\Deal;
  5. use App\Entity\Project;
  6. use App\Entity\Proposal;
  7. use App\Event\ProposalCanceledEvent;
  8. use App\Event\ProposalFundedEvent;
  9. use App\Repository\Message\MessageReceptionRepository;
  10. use App\Service\NotificationMessageProvider;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProposalNotificationSubscriber implements EventSubscriberInterface
  13. {
  14.     private NotificationMessageProvider $notificationMessageProvider;
  15.     private TaggedMemcachedCache $taggedMemcachedCache;
  16.     private MessageReceptionRepository $messageReceptionRepository;
  17.     public function __construct(NotificationMessageProvider $notificationMessageProviderMessageReceptionRepository $messageReceptionRepositoryTaggedMemcachedCache $taggedMemcachedCache)
  18.     {
  19.         $this->notificationMessageProvider $notificationMessageProvider;
  20.         $this->taggedMemcachedCache $taggedMemcachedCache;
  21.         $this->messageReceptionRepository $messageReceptionRepository;
  22.     }
  23.     public function onFunded(ProposalFundedEvent $event): void
  24.     {
  25.         $proposal $event->getProposal();
  26.         if ($proposal instanceof Deal) {
  27.             $bidders $this->getBiddersToNotify($proposal);
  28.             $dealSuccessMessage $this->notificationMessageProvider->getDealSuccessfulNotification();
  29.             assert(null !== $dealSuccessMessage->getId());
  30.             $this->messageReceptionRepository->sendTo($dealSuccessMessage->getId(), $bidders$this->taggedMemcachedCachenull, [
  31.                 'proposalTitle' => $proposal->getTitle(),
  32.                 'proposalStringId' => $proposal->getStringId(),
  33.             ]);
  34.         }
  35.     }
  36.     public function onCanceled(ProposalCanceledEvent $event): void
  37.     {
  38.         $proposal $event->getProposal();
  39.         if ($proposal instanceof Deal) {
  40.             $bidders $this->getBiddersToNotify($proposal);
  41.             $dealFailedMessage $this->notificationMessageProvider->getDealFailedNotification();
  42.             assert(null !== $dealFailedMessage->getId());
  43.             $this->messageReceptionRepository->sendTo($dealFailedMessage->getId(), $bidders$this->taggedMemcachedCachenull, [
  44.                 'proposalTitle' => $proposal->getTitle(),
  45.                 'proposalStringId' => $proposal->getStringId(),
  46.             ]);
  47.         }
  48.     }
  49.     /**
  50.      * @param Project|Deal $proposal
  51.      * @return array<int, int> $bidders
  52.      */
  53.     private function getBiddersToNotify(Proposal $proposal): array
  54.     {
  55.         $bidders = [];
  56.         foreach ($proposal->getBids() as $proposalBid) {
  57.             $bidUser $proposalBid->getUser();
  58.             if (null === $bidUser->getId()) {
  59.                 continue;
  60.             }
  61.             if (isset($bidders[$bidUser->getId()])) {
  62.                 continue;
  63.             }
  64.             if (!$proposalBid->isJustRefunded() && ($proposalBid->isRefunded() || $proposalBid->isCanceled())) {
  65.                 continue;
  66.             }
  67.             $bidders[] = $bidUser->getId();
  68.         }
  69.         return $bidders;
  70.     }
  71.     /**
  72.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  73.      */
  74.     public static function getSubscribedEvents(): array
  75.     {
  76.         return [
  77.             ProposalFundedEvent::class => 'onFunded',
  78.             ProposalCanceledEvent::class => 'onCanceled',
  79.         ];
  80.     }
  81. }