<?php declare(strict_types=1);
namespace App\Subscriber;
use App\Entity\ShopVideoBid;
use App\Event\CreditsEvent;
use LogicException;
use RuntimeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
class ShopVideoCreditsSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function onCredits(CreditsEvent $event): void
{
if ($event->getAction() !== CreditsEvent::ACTION_SHOP_VIDEO_BID) {
return;
}
$proposalBid = $event->getRelatedEntity();
if (!($proposalBid instanceof ShopVideoBid)) {
throw new LogicException('The related entity should be ShopVideoBid.');
}
$session = $this->requestStack->getSession();
if (!$session instanceof Session) {
throw new RuntimeException('The application requires Session.');
}
$session->getFlashBag()->add(
'purchase-success',
$event->getCreditsFloat() * -1
);
}
/**
* @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
*/
public static function getSubscribedEvents(): array
{
return [
CreditsEvent::class => 'onCredits',
];
}
}