Completed
Push — master ( 13edef...d5b56b )
by Jeroen
85:52 queued 71:18
created

ExceptionSubscriber::onKernelException()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 40

Duplication

Lines 3
Ratio 7.5 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 3
loc 40
ccs 0
cts 22
cp 0
rs 7.6666
c 0
b 0
f 0
cc 10
nc 13
nop 1
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
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...
27 3
     */
28
    public static function getSubscribedEvents()
29
    {
30 3
        return [
31
            KernelEvents::EXCEPTION => 'onKernelException',
32
        ];
33
    }
34
35
    /**
36
     * ExceptionSubscriber constructor.
37
     *
38
     * @param EntityManagerInterface $em
39
     * @param array                  $excludes
40
     */
41
    public function __construct(EntityManagerInterface $em, array $excludes = [])
42
    {
43
        $this->em = $em;
44
        $this->excludes = $excludes;
45
    }
46
47
    /**
48
     * @param GetResponseForExceptionEvent|ExceptionEvent $event
49
     */
50
    public function onKernelException($event)
51
    {
52 View Code Duplication
        if (!$event instanceof GetResponseForExceptionEvent && !$event instanceof ExceptionEvent) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            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)));
54
        }
55
56
        $request = $event->getRequest();
57
        $exception = \method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...onEvent::getException() has been deprecated with message: since Symfony 4.4, use getThrowable instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
59
        if ($exception instanceof HttpExceptionInterface) {
60
            $uri = $request->getUri();
61
62
            if (count($this->excludes) > 0) {
63
                $excludes = array_filter($this->excludes, function ($pattern) use ($uri) {
64
                    return preg_match($pattern, $uri);
65
                });
66
67
                if (count($excludes) > 0) {
68
                    return;
69
                }
70
            }
71
72
            $hash = md5(
73
                $exception->getStatusCode().$uri.$request->headers->get('referer')
74
            );
75
76
            if ($model = $this->em->getRepository(Exception::class)->findOneBy(['hash' => $hash])) {
77
                $model->increaseEvents();
78
                $model->setResolved(false);
79
            } else {
80
                $model = new Exception();
81
                $model->setCode($exception->getStatusCode());
82
                $model->setUrl($uri);
83
                $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 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...
84
                $model->setHash($hash);
85
            }
86
            $this->em->persist($model);
87
            $this->em->flush();
88
        }
89
    }
90
}
91