src/EventListener/ExceptionListener.php line 64

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\EventListener;
  3. use Harmonizely\Core\Exception\BaseException;
  4. use Harmonizely\Core\Exception\Contract\IBaseException;
  5. use Harmonizely\Core\Exception\RedirectToRoute;
  6. use Harmonizely\Core\Exception\UnexpectedException;
  7. use Harmonizely\Core\Response\Contract\IResponseBuilder;
  8. use Harmonizely\Service\UserLoginAttempt\Exception\AccessDeniedException as AccessDeniedExceptionUserLoginAttempt;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. class ExceptionListener
  15. {
  16.     /**
  17.      * @var SessionInterface
  18.      */
  19.     private SessionInterface $session;
  20.     /**
  21.      * @var UrlGeneratorInterface
  22.      */
  23.     private UrlGeneratorInterface $urlGenerator;
  24.     /**
  25.      * @var IResponseBuilder
  26.      */
  27.     private IResponseBuilder $responseBuilder;
  28.     /**
  29.      * @var LoggerInterface
  30.      */
  31.     private LoggerInterface $logger;
  32.     /**
  33.      * @param SessionInterface $session
  34.      * @param UrlGeneratorInterface $urlGenerator
  35.      * @param IResponseBuilder $responseBuilder
  36.      * @param LoggerInterface $logger
  37.      */
  38.     public function __construct(
  39.         SessionInterface $session,
  40.         UrlGeneratorInterface $urlGenerator,
  41.         IResponseBuilder $responseBuilder,
  42.         LoggerInterface $logger
  43.     )
  44.     {
  45.         $this->session $session;
  46.         $this->urlGenerator $urlGenerator;
  47.         $this->responseBuilder $responseBuilder;
  48.         $this->logger $logger;
  49.     }
  50.     /**
  51.      * Handle kernel exception event
  52.      *
  53.      * @param ExceptionEvent $event
  54.      */
  55.     public function onKernelException(ExceptionEvent $event)
  56.     {
  57.         $exception $event->getThrowable();
  58.         if ($exception instanceof IBaseException) {
  59.             $event->setResponse($this->responseBuilder->buildExceptionResponse($exception));
  60.             return;
  61.         }
  62.         if ($exception instanceof AccessDeniedExceptionUserLoginAttempt) {
  63.             $this->session->getFlashBag()->add('danger'$exception->getMessage());
  64.             $event->setResponse(new RedirectResponse($this->urlGenerator->generate('fos_user_security_login')));
  65.             return;
  66.         }
  67.         if ($exception instanceof RedirectToRoute) {
  68.             $event->setResponse(new RedirectResponse($this->urlGenerator->generate($exception->getRoute())));
  69.             return;
  70.         }
  71.         $this->logger->error('Exception occurred: ' $exception->getMessage(), [
  72.             'exception' => $exception,
  73.             'trace'     => $exception->getTraceAsString(),
  74.         ]);
  75.         if ($exception instanceof UnexpectedException) {
  76.             $event->setResponse($this->responseBuilder->buildExceptionResponse(new BaseException($exception->getMessage())));
  77.             return;
  78.         }
  79.     }
  80. }