<?php
namespace App\Events;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use App\Mybase\Services\Base\SBase;
use App\Services\Exception\AgainstProposalCounterOfferException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class DesignNeighborhoodExceptionSubscriber implements EventSubscriberInterface
{
/**
* @var SBase
*/
private $SBase;
/**
* DesignNeighborhoodExceptionSubscriber constructor.
* @param SBase $SBase
*/
public function __construct(SBase $SBase)
{
$this->SBase = $SBase;
}
public static function getSubscribedEvents(): array
{
return [
ExceptionEvent::class => 'onKernelException',
];
}
/**
* @param ExceptionEvent $event
* @return void
*/
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
$request = $event->getRequest();
if ($exception instanceof ItemNotFoundException && $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);
}
}
}