Completed
Push — 5.3 ( 958546...1cc96e )
by Jeroen
14:02 queued 07:05
created

AdminBundle/EventListener/ExceptionSubscriber.php (1 issue)

Labels
Severity

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
26
     */
27 3
    public static function getSubscribedEvents()
28
    {
29
        return [
30 3
            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
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