Test Failed
Push — master ( 2f32dc...85d4e6 )
by Zbigniew
03:09
created

SystemRequestSubscriber   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 24.55 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 86.79%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 11
dl 27
loc 110
ccs 46
cts 53
cp 0.8679
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 14 1
B onKernelRequest() 0 48 5
A onKernelResponse() 14 14 3
A onKernelException() 13 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/sharep.
7
 *
8
 * (c) Zbigniew Ślązak
9
 */
10
11
namespace App\EventSubscriber;
12
13
use App\Entity\Access\User;
14
use App\Entity\System\RequestLog;
15
use App\Entity\System\RequestLogDetail;
16
use App\Enum\Entity\RequestLogTypeEnum;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
20
use Symfony\Component\HttpKernel\Event\RequestEvent;
21
use Symfony\Component\HttpKernel\Event\ResponseEvent;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
25
26
class SystemRequestSubscriber implements EventSubscriberInterface
27
{
28
    /** @var EntityManagerInterface */
29
    private $entityManager;
30
    /** @var TokenStorageInterface TokenStorageInterface */
31
    private $tokenStorage;
32
    /** @var RequestLog|null */
33
    private $requestLog;
34
    /** @var RequestLogDetail|null */
35
    private $requestLogDetail;
36
37 21
    public function __construct(EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage)
38
    {
39 21
        $this->entityManager = $entityManager;
40 21
        $this->tokenStorage = $tokenStorage;
41 21
    }
42
43 1
    public static function getSubscribedEvents(): array
44
    {
45
        return [
46
            KernelEvents::REQUEST => [
47 1
                ['onKernelRequest', 0],
48
            ],
49
            KernelEvents::RESPONSE => [
50
                ['onKernelResponse', 0],
51
            ],
52
            KernelEvents::EXCEPTION => [
53
                ['onKernelException', 0],
54
            ],
55
        ];
56
    }
57
58 16
    public function onKernelRequest(RequestEvent $event): void
59
    {
60 16
        if (!$event->isMasterRequest()) {
61
            return;
62
        }
63
64 16
        $request = $event->getRequest();
65
66 16
        if ('_wdt' === $request->attributes->get('_route')) {
67
            return;
68
        }
69
70 16
        $member = null;
71 16
        $token = $this->tokenStorage->getToken();
72 16
        if ($token instanceof TokenInterface) {
73
            /** @var User $user */
74 16
            $user = $token->getUser();
75 16
            if ($user instanceof User) {
76 12
                $user = $this->entityManager->find(User::class, $user->getId());
77 12
                $member = $user->getMember();
78
            }
79
        }
80
81 16
        $this->requestLog = new RequestLog(
82 16
            RequestLogTypeEnum::HTTP(),
83 16
            $request->getPathInfo(),
84 16
            new \DateTimeImmutable(),
85
            $member
86
        );
87
88 16
        $this->requestLogDetail = new RequestLogDetail(
89 16
            $this->requestLog,
90 16
            $request->getRequestUri(),
91 16
            json_encode(
92
                [
93 16
                    'query' => $request->query->all(),
94 16
                    'request' => $request->request->all(),
95 16
                    'attributes' => $request->attributes->all(),
96 16
                    'headers' => $request->headers->all(),
97
                ],
98 16
                JSON_THROW_ON_ERROR
99
            )
100
        );
101
102 16
        $this->entityManager->persist($this->requestLog);
103 16
        $this->entityManager->persist($this->requestLogDetail);
104 16
        $this->entityManager->flush();
105 16
    }
106
107 21 View Code Duplication
    public function onKernelResponse(ResponseEvent $event): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
108
    {
109 21
        if (!$event->isMasterRequest()) {
110
            return;
111
        }
112
113 21
        if ($this->requestLog) {
114 16
            $this->requestLog->setFinishedAt(new \DateTimeImmutable());
115 16
            $this->requestLog->setSuccessful(true);
116
117 16
            $this->entityManager->persist($this->requestLog);
118 16
            $this->entityManager->flush();
119
        }
120 21
    }
121
122 3 View Code Duplication
    public function onKernelException(ExceptionEvent $event): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
123
    {
124 3
        if (!$event->isMasterRequest()) {
125
            return;
126
        }
127
128 3
        if (null === $this->requestLog) {
129 3
            $this->requestLog->setSuccessful(false);
0 ignored issues
show
Bug introduced by
The method setSuccessful cannot be called on $this->requestLog (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
130
131
            $this->entityManager->persist($this->requestLog);
0 ignored issues
show
Documentation introduced by
$this->requestLog is of type null, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
            $this->entityManager->flush();
133
        }
134
    }
135
}
136