<?php
namespace App\EventSubscriber;
use App\Exception\CurrentUserException;
use App\Mybase\Services\Base\SBase;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class CurrentUserExceptionSubscriber implements EventSubscriberInterface
{
/**
* @var sBase
*/
private $sBase;
public function __construct(SBase $SBase)
{
$this->sBase = $SBase;
}
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
$request = $event->getRequest();
if ($exception instanceof CurrentUserException && $request->isMethod(Request::METHOD_POST)) {
$response = $this->sBase->jsonResponseBadRequest($exception->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
$event->setResponse($response);
}
}
public static function getSubscribedEvents(): array
{
return [
'kernel.exception' => 'onKernelException',
];
}
}