Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
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\GetResponseForExceptionEvent;
9
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
class ExceptionSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @var EntityManagerInterface
16
     */
17
    private $em;
18
19
    /**
20
     * @var array
21
     */
22
    private $excludes;
23
24
    /**
25
     * @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...
26
     */
27
    public static function getSubscribedEvents()
28
    {
29
        return [
30
            KernelEvents::EXCEPTION => 'onKernelException',
31
        ];
32
    }
33
34
    /**
35
     * ExceptionSubscriber constructor.
36
     *
37
     * @param EntityManagerInterface $em
38
     * @param array                  $excludes
39
     */
40
    public function __construct(EntityManagerInterface $em, array $excludes = [])
41
    {
42
        $this->em = $em;
43
        $this->excludes = $excludes;
44
    }
45
46
    /**
47
     * @param GetResponseForExceptionEvent $event
48
     */
49
    public function onKernelException(GetResponseForExceptionEvent $event)
50
    {
51
        $request = $event->getRequest();
52
        $exception = $event->getException();
53
54
        if ($exception instanceof HttpExceptionInterface) {
55
            $uri = $request->getUri();
56
57
            if (count($this->excludes) > 0) {
58
                $excludes = array_filter($this->excludes, function ($pattern) use ($uri) {
59
                    return preg_match($pattern, $uri);
60
                });
61
62
                if (count($excludes) > 0) {
63
                    return;
64
                }
65
            }
66
67
            $hash = md5(
68
                $exception->getStatusCode().$uri.$request->headers->get('referer')
69
            );
70
71
            if ($model = $this->em->getRepository(Exception::class)->findOneBy(['hash' => $hash])) {
72
                $model->increaseEvents();
73
                $model->setResolved(false);
74
            } else {
75
                $model = new Exception();
76
                $model->setCode($exception->getStatusCode());
77
                $model->setUrl($uri);
78
                $model->setUrlReferer($request->headers->get('referer'));
79
                $model->setHash($hash);
80
            }
81
            $this->em->persist($model);
82
            $this->em->flush();
83
        }
84
    }
85
}
86