Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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
     * @param EntityManagerInterface $em
37
     * @param array $excludes
38
     */
39
    public function __construct(EntityManagerInterface $em, array $excludes = [])
40
    {
41
        $this->em = $em;
42
        $this->excludes = $excludes;
43
    }
44
45
    /**
46
     * @param GetResponseForExceptionEvent $event
47
     */
48
    public function onKernelException(GetResponseForExceptionEvent $event)
49
    {
50
        $request = $event->getRequest();
51
        $exception = $event->getException();
52
53
        if ($exception instanceof HttpExceptionInterface) {
54
            $uri = $request->getUri();
55
56
            if(count($this->excludes) > 0) {
57
                $excludes = array_filter($this->excludes, function($pattern) use($uri) {
58
                   return preg_match($pattern, $uri);
59
                });
60
61
                if(count($excludes) > 0) {
62
                    return ;
63
                }
64
            }
65
66
            $hash = md5(
67
                $exception->getStatusCode().$uri.$request->headers->get('referer')
68
            );
69
70
            if ($model = $this->em->getRepository(Exception::class)->findOneBy(['hash' => $hash])) {
71
                $model->increaseEvents();
72
                $model->setResolved(false);
73
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
            }
82
            $this->em->persist($model);
83
            $this->em->flush();
84
        }
85
    }
86
}
87