Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

ExceptionSubscriber::onKernelException()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.0328
c 0
b 0
f 0
cc 5
nc 6
nop 1
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
Documentation introduced by
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'));
0 ignored issues
show
Bug introduced by
It seems like $request->headers->get('referer') targeting Symfony\Component\HttpFoundation\HeaderBag::get() can also be of type array<integer,string> or null; however, Kunstmaan\AdminBundle\En...eption::setUrlReferer() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79
                $model->setHash($hash);
80
            }
81
            $this->em->persist($model);
82
            $this->em->flush();
83
        }
84
    }
85
}
86