src/InstallBundle/Listener/ExceptionListener.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of SolidInvoice project.
  5.  *
  6.  * (c) Pierre du Plessis <open-source@solidworx.co>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace SolidInvoice\InstallBundle\Listener;
  12. use SolidInvoice\InstallBundle\Exception\ApplicationInstalledException;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  16. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class ExceptionListener implements EventSubscriberInterface
  21. {
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             KernelEvents::EXCEPTION => 'onKernelException',
  26.         ];
  27.     }
  28.     public function __construct(
  29.         private readonly FlashBagInterface $flashBag,
  30.         private readonly TranslatorInterface $translator,
  31.         private readonly RouterInterface $router
  32.     ) {
  33.     }
  34.     public function onKernelException(ExceptionEvent $event): void
  35.     {
  36.         $exception $event->getThrowable();
  37.         if ($exception instanceof ApplicationInstalledException) {
  38.             $this->flashBag->add('error'$this->translator->trans($exception->getMessage()));
  39.             $event->setResponse(new RedirectResponse($this->router->generate('_home')));
  40.             $event->stopPropagation();
  41.         }
  42.     }
  43. }