src/EventSubscriber/InsufficientFundsExceptionEventSubscriber.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Exception\InsufficientFundsException;
  4. use Junker\JSendResponse\JSendFailResponse;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class InsufficientFundsExceptionEventSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(private RequestStack $requestStack)
  12.     {
  13.     }
  14.     public function onKernelException(ExceptionEvent $event): void
  15.     {
  16.         $exception $event->getThrowable();
  17.         if (!($exception instanceof InsufficientFundsException)) {
  18.             return;
  19.         }
  20.         if (!$this->requestStack->getMainRequest()) {
  21.             return;
  22.         }
  23.         $event->setResponse(
  24.             new JSendFailResponse('Player has insufficient funds to execute this action.'JSendFailResponse::HTTP_PAYMENT_REQUIRED)
  25.         );
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             KernelEvents::EXCEPTION => 'onKernelException',
  31.         ];
  32.     }
  33. }