vendor/kunstmaan/admin-bundle/EventListener/ExceptionSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\AdminBundle\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Kunstmaan\AdminBundle\Entity\Exception;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  8. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class ExceptionSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var EntityManagerInterface
  14.      */
  15.     private $em;
  16.     /**
  17.      * @var array
  18.      */
  19.     private $excludes;
  20.     /**
  21.      * @return array
  22.      */
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             KernelEvents::EXCEPTION => 'onKernelException',
  27.         ];
  28.     }
  29.     /**
  30.      * ExceptionSubscriber constructor.
  31.      *
  32.      * @param EntityManagerInterface $em
  33.      * @param array                  $excludes
  34.      */
  35.     public function __construct(EntityManagerInterface $em, array $excludes = [])
  36.     {
  37.         $this->em $em;
  38.         $this->excludes $excludes;
  39.     }
  40.     /**
  41.      * @param GetResponseForExceptionEvent|ExceptionEvent $event
  42.      */
  43.     public function onKernelException($event)
  44.     {
  45.         if (!$event instanceof GetResponseForExceptionEvent && !$event instanceof ExceptionEvent) {
  46.             throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ExceptionEvent::class) ? ExceptionEvent::class : GetResponseForExceptionEvent::class, \is_object($event) ? \get_class($event) : \gettype($event)));
  47.         }
  48.         $request $event->getRequest();
  49.         $exception = \method_exists($event'getThrowable') ? $event->getThrowable() : $event->getException();
  50.         if ($exception instanceof HttpExceptionInterface) {
  51.             $uri $request->getUri();
  52.             if (\count($this->excludes) > 0) {
  53.                 $excludes array_filter($this->excludes, function ($pattern) use ($uri) {
  54.                     return preg_match($pattern$uri);
  55.                 });
  56.                 if (\count($excludes) > 0) {
  57.                     return;
  58.                 }
  59.             }
  60.             $hash md5(
  61.                 $exception->getStatusCode().$uri.$request->headers->get('referer')
  62.             );
  63.             if ($model $this->em->getRepository(Exception::class)->findOneBy(['hash' => $hash])) {
  64.                 $model->increaseEvents();
  65.                 $model->setResolved(false);
  66.             } else {
  67.                 $model = new Exception();
  68.                 $model->setCode($exception->getStatusCode());
  69.                 $model->setUrl($uri);
  70.                 $model->setUrlReferer($request->headers->get('referer'));
  71.                 $model->setHash($hash);
  72.             }
  73.             $this->em->persist($model);
  74.             $this->em->flush();
  75.         }
  76.     }
  77. }