src/EventListener/AppMessageExceptionSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Exception\AppExceptionInterface;
  4. use App\Exception\SimpleMessageException;
  5. use Psr\Container\ContainerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. class AppMessageExceptionSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var ContainerInterface
  16.      */
  17.     private $container;
  18.     /**
  19.      * @var UrlGeneratorInterface
  20.      */
  21.     private $urlGenerator;
  22.     
  23.     public function __construct(ContainerInterface $container)
  24.     {
  25.         $this->container $container;
  26.     }
  27.     
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return array(
  31.             KernelEvents::EXCEPTION => 'onKernelException'
  32.         );
  33.     }
  34.     
  35.     public function onKernelException(ExceptionEvent $event)
  36.     {
  37.         $url $event->getRequest()->headers->get('referer');
  38.         $e $event->getThrowable();
  39.         if (!$e instanceof AppExceptionInterface &&
  40.               !$e instanceof AccessDeniedHttpException ) {
  41.             return;
  42.         }
  43.         
  44.         $this->container->get('session')->getFlashBag()->add('error'$e->getMessage());
  45.         if($url === null){
  46.             $url $this->container->get('router')->generate('app_homepage');
  47.         }
  48.         $event->setResponse(new RedirectResponse($url));
  49.         
  50.         return $event;
  51.     }
  52.     
  53. }