src/App/Subscriber/ShopVideoCreditsSubscriber.php line 23

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use App\Entity\ShopVideoBid;
  4. use App\Event\CreditsEvent;
  5. use LogicException;
  6. use RuntimeException;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. class ShopVideoCreditsSubscriber implements EventSubscriberInterface
  11. {
  12.     private RequestStack $requestStack;
  13.     public function __construct(RequestStack $requestStack)
  14.     {
  15.         $this->requestStack $requestStack;
  16.     }
  17.     public function onCredits(CreditsEvent $event): void
  18.     {
  19.         if ($event->getAction() !== CreditsEvent::ACTION_SHOP_VIDEO_BID) {
  20.             return;
  21.         }
  22.         $proposalBid $event->getRelatedEntity();
  23.         if (!($proposalBid instanceof ShopVideoBid)) {
  24.             throw new LogicException('The related entity should be ShopVideoBid.');
  25.         }
  26.         $session $this->requestStack->getSession();
  27.         if (!$session instanceof Session) {
  28.             throw new RuntimeException('The application requires Session.');
  29.         }
  30.         $session->getFlashBag()->add(
  31.             'purchase-success',
  32.             $event->getCreditsFloat() * -1
  33.         );
  34.     }
  35.     /**
  36.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             CreditsEvent::class => 'onCredits',
  42.         ];
  43.     }
  44. }