<?php
namespace App\EventSubscriber;
use App\Exception\OutdatedItemsVersionException;
use App\Service\NotificationService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ExceptionSubscriber implements EventSubscriberInterface
{
private $notificationService;
private $em;
public function __construct(NotificationService $notificationService, EntityManagerInterface $entityManager)
{
$this->notificationService = $notificationService;
$this->em = $entityManager;
}
public static function getSubscribedEvents()
{
return [ KernelEvents::EXCEPTION => 'onKernelException' ];
}
public function onKernelException(ExceptionEvent $event)
{
if($event->getException() instanceof OutdatedItemsVersionException){
$customResponse = new JsonResponse([
'error' => $event->getException()->getMessage(),
'actual_version' => "v{$event->getException()->getCode()}"
]);
$event->setResponse($customResponse);
}
}
}