<?php
namespace App\EventSubscriber;
use App\Exception\InsufficientFundsException;
use Junker\JSendResponse\JSendFailResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class InsufficientFundsExceptionEventSubscriber implements EventSubscriberInterface
{
public function __construct(private RequestStack $requestStack)
{
}
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!($exception instanceof InsufficientFundsException)) {
return;
}
if (!$this->requestStack->getMainRequest()) {
return;
}
$event->setResponse(
new JSendFailResponse('Player has insufficient funds to execute this action.', JSendFailResponse::HTTP_PAYMENT_REQUIRED)
);
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => 'onKernelException',
];
}
}