These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 |
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 |
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
|
|||
130 | |||
131 | $this->entityManager->persist($this->requestLog); |
||
132 | $this->entityManager->flush(); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.