Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AdminBundle/EventListener/ExceptionSubscriber.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\EventListener;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\Exception;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
9
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
class ExceptionSubscriber implements EventSubscriberInterface
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $em;
19
20
    /**
21
     * @var array
22
     */
23
    private $excludes;
24
25
    /**
26
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<*,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
27
     */
28 3
    public static function getSubscribedEvents()
29
    {
30
        return [
31 3
            KernelEvents::EXCEPTION => 'onKernelException',
32
        ];
33
    }
34
35
    /**
36
     * ExceptionSubscriber constructor.
37
     */
38
    public function __construct(EntityManagerInterface $em, array $excludes = [])
39
    {
40
        $this->em = $em;
41
        $this->excludes = $excludes;
42
    }
43
44
    /**
45
     * @param GetResponseForExceptionEvent|ExceptionEvent $event
46
     */
47
    public function onKernelException($event)
48
    {
49 View Code Duplication
        if (!$event instanceof GetResponseForExceptionEvent && !$event instanceof ExceptionEvent) {
50
            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)));
51
        }
52
53
        $request = $event->getRequest();
54
        $exception = \method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
55
56
        if ($exception instanceof HttpExceptionInterface) {
57
            $uri = $request->getUri();
58
59
            if (\count($this->excludes) > 0) {
60
                $excludes = array_filter($this->excludes, function ($pattern) use ($uri) {
61
                    return preg_match($pattern, $uri);
62
                });
63
64
                if (\count($excludes) > 0) {
65
                    return;
66
                }
67
            }
68
69
            $hash = md5(
70
                $exception->getStatusCode() . $uri . $request->headers->get('referer')
71
            );
72
73
            if ($model = $this->em->getRepository(Exception::class)->findOneBy(['hash' => $hash])) {
74
                $model->increaseEvents();
75
                $model->setResolved(false);
76
            } else {
77
                $model = new Exception();
78
                $model->setCode($exception->getStatusCode());
79
                $model->setUrl($uri);
80
                $model->setUrlReferer($request->headers->get('referer'));
81
                $model->setHash($hash);
82
            }
83
            $this->em->persist($model);
84
            $this->em->flush();
85
        }
86
    }
87
}
88