src/EventSubscriber/CurrentUserExceptionSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Exception\CurrentUserException;
  4. use App\Mybase\Services\Base\SBase;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. class CurrentUserExceptionSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var sBase
  13.      */
  14.     private $sBase;
  15.     public function __construct(SBase $SBase)
  16.     {
  17.         $this->sBase $SBase;
  18.     }
  19.     public function onKernelException(ExceptionEvent $event)
  20.     {
  21.         $exception $event->getThrowable();
  22.         $request $event->getRequest();
  23.         if ($exception instanceof CurrentUserException && $request->isMethod(Request::METHOD_POST)) {
  24.             $response $this->sBase->jsonResponseBadRequest($exception->getMessage());
  25.             $response->setStatusCode(Response::HTTP_NOT_FOUND);
  26.             $event->setResponse($response);
  27.         }
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             'kernel.exception' => 'onKernelException',
  33.         ];
  34.     }
  35. }