src/App/Subscriber/ProposalCreditsSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use App\Entity\Deal;
  4. use App\Entity\DealBid;
  5. use App\Entity\Project;
  6. use App\Entity\ProjectBid;
  7. use App\Entity\ProposalBid;
  8. use App\Event\CreditsEvent;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use LogicException;
  11. use RuntimeException;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. class ProposalCreditsSubscriber implements EventSubscriberInterface
  16. {
  17.     private ManagerRegistry $doctrine;
  18.     private RequestStack $requestStack;
  19.     public function __construct(ManagerRegistry $doctrineRequestStack $requestStack)
  20.     {
  21.         $this->doctrine $doctrine;
  22.         $this->requestStack $requestStack;
  23.     }
  24.     public function onCredits(CreditsEvent $event): void
  25.     {
  26.         if (!in_array($event->getAction(), [CreditsEvent::ACTION_DEAL_BIDCreditsEvent::ACTION_DEAL_BID_REFUNDCreditsEvent::ACTION_PROJECT_BIDCreditsEvent::ACTION_PROJECT_BID_REFUND], true)) {
  27.             return;
  28.         }
  29.         $proposalBid $event->getRelatedEntity();
  30.         if (!($proposalBid instanceof ProposalBid)) {
  31.             throw new LogicException('The related entity should be ProposalBid.');
  32.         }
  33.         if ($proposalBid instanceof DealBid) {
  34.             $this->doctrine->getRepository(Deal::class)->addContribution(
  35.                 $proposalBid->getDeal(),
  36.                 $event->getCredits() * -1
  37.             );
  38.         }
  39.         if ($proposalBid instanceof ProjectBid) {
  40.             $this->doctrine->getRepository(Project::class)->addContribution(
  41.                 $proposalBid->getProject(),
  42.                 $event->getCredits() * -1
  43.             );
  44.         }
  45.         if (!in_array($event->getAction(), [CreditsEvent::ACTION_DEAL_BIDCreditsEvent::ACTION_PROJECT_BID], true)) {
  46.             return;
  47.         }
  48.         $session $this->requestStack->getSession();
  49.         if (!$session instanceof Session) {
  50.             throw new RuntimeException('The application requires Session.');
  51.         }
  52.         $session->getFlashBag()->add(
  53.             'contribution-success',
  54.             $event->getCreditsFloat() * -1
  55.         );
  56.     }
  57.     /**
  58.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  59.      */
  60.     public static function getSubscribedEvents(): array
  61.     {
  62.         return [
  63.             CreditsEvent::class => 'onCredits',
  64.         ];
  65.     }
  66. }