<?php
namespace App\EventSubscriber;
use App\Exception\MissingParameterRequestDataException;
use App\Mybase\Services\Base\SBase;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class MissingParameterRequestDataExceptionSubscriber implements EventSubscriberInterface
{
/**
* @var SBase
*/
private $SBase;
public function __construct(SBase $SBase)
{
$this->SBase = $SBase;
}
public static function getSubscribedEvents(): array
{
return [
'kernel.exception' => 'onKernelException',
];
}
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
$request = $event->getRequest();
if ($exception instanceof MissingParameterRequestDataException && $request->isMethod('POST')) {
$response = $this->SBase->jsonResponseFailedCondition($exception->getMessage());
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
// setup the Response object based on the caught exception
$event->setResponse($response);
}
}
}