src/EventSubscriber/DesignNeighborhoodExceptionSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Exception\ItemNotFoundException;
  4. use App\Mybase\Services\Base\SBase;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. class DesignNeighborhoodExceptionSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var SBase
  12.      */
  13.     private $SBase;
  14.     /**
  15.      * DesignNeighborhoodExceptionSubscriber constructor.
  16.      */
  17.     public function __construct(SBase $SBase)
  18.     {
  19.         $this->SBase $SBase;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ExceptionEvent::class => 'onKernelException',
  25.         ];
  26.     }
  27.     /**
  28.      * @return void
  29.      */
  30.     public function onKernelException(ExceptionEvent $event)
  31.     {
  32.         $exception $event->getThrowable();
  33.         $request $event->getRequest();
  34.         if ($exception instanceof ItemNotFoundException && $request->isMethod('POST')) {
  35.             $response $this->SBase->jsonResponseFailedCondition($exception->getMessage());
  36.             $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  37.             // setup the Response object based on the caught exception
  38.             $event->setResponse($response);
  39.         }
  40.     }
  41. }