src/EventListener/ExceptionSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Twig\Environment;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. class ExceptionSubscriber implements EventSubscriberInterface
  14. {
  15.     private $user;
  16.     private $twig;
  17.     private $container;
  18.         private $em;
  19.         private $security;
  20.     public function __construct(Security $security,EntityManagerInterface $em,Environment $twigContainerInterface $containerUserInterface $user null)
  21.     {
  22.         $this->user $user;
  23.         $this->twig $twig;
  24.         $this->container $container;
  25.         $this->em $em;
  26.             $this->security $security;
  27.     }
  28.        public function onTerminate() {
  29.            $user $this->security->getUser();
  30.            if ($user && !$user->isActiveNow()) {
  31.               $user->setlastActivity(new \DateTime());
  32.               $this->em->persist($user);
  33.                 $this->em->flush($user);
  34.               }
  35.         }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.                     KernelEvents::TERMINATE => [['onTerminate'20]],
  40.             KernelEvents::EXCEPTION => [
  41.                 ['onKernelException'10]
  42.             ]
  43.         ];
  44.     }
  45.     public function onKernelException(ExceptionEvent $event)
  46.     {
  47.         // We only need to suppress errors in production environment
  48.         if ($this->container->get('kernel')->getEnvironment() != 'prod') {
  49.             return;
  50.         }
  51.         // @TODO: We need to take into account the response type as well (html, xml, json)
  52.         $exception $event->getException();
  53.         if ($exception->getCode() == 403) {
  54.             // On forbidden exception, we need to either:
  55.             //         a) If user session is set, display forbidden page
  56.             //         b) If user session is not set, redirect to login page
  57.             if (!empty($this->user)) {
  58.                 $template $this->twig->render('errors/error.html.twig', [
  59.                     'code' => 403,
  60.                     'message' => 'Access Forbidden',
  61.                     'description' => 'You are not authorized to access this page.',
  62.                 ]);
  63.     
  64.                 $event->setResponse(new Response($template403));
  65.             }
  66.         } else {
  67.             if ($exception instanceof NotFoundHttpException) {
  68.                 $template $this->twig->render('errors/error.html.twig', [
  69.                     'code' => 404,
  70.                     'message' => 'Page not Found',
  71.                     'description' => 'We were not able to find the page you are looking for.',
  72.                 ]);
  73.     
  74.                 $event->setResponse(new Response($template404));
  75.             } else {
  76.                 $template $this->twig->render('errors/error.html.twig', [
  77.                     'message' => 'Internal Server Error',
  78.                     'code' => 500,
  79.                     'description' => 'Something has gone wrong on the server. Please try again later.',
  80.                 ]);
  81.     
  82.                 $event->setResponse(new Response($template500));
  83.             }
  84.         }
  85.     }
  86. }