src/EventSubscriber/ExceptionSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Exception\OutdatedItemsVersionException;
  4. use App\Service\NotificationService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class ExceptionSubscriber implements EventSubscriberInterface
  11. {
  12.   private $notificationService;
  13.   private $em;
  14.   public function __construct(NotificationService $notificationServiceEntityManagerInterface $entityManager)
  15.   {
  16.     $this->notificationService $notificationService;
  17.     $this->em $entityManager;
  18.   }
  19.   public static function getSubscribedEvents()
  20.   {
  21.     return [ KernelEvents::EXCEPTION => 'onKernelException' ];
  22.   }
  23.   public function onKernelException(ExceptionEvent $event)
  24.   {
  25.     if($event->getException() instanceof OutdatedItemsVersionException){
  26.       $customResponse = new JsonResponse([
  27.         'error' => $event->getException()->getMessage(),
  28.         'actual_version' => "v{$event->getException()->getCode()}"
  29.       ]);
  30.       $event->setResponse($customResponse);
  31.     }
  32.   }
  33. }