src/Events/DesignNeighborhoodExceptionSubscriber.php line 39

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