Passed
Push — master ( 3de5a0...6f9405 )
by Tarmo
11:46
created

RequestLogger::setApiKeyId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Utils/RequestLogger.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Utils;
10
11
use App\Entity\LogRequest;
12
use App\Resource\ApiKeyResource;
13
use App\Resource\LogRequestResource;
14
use App\Resource\UserResource;
15
use App\Utils\Interfaces\RequestLoggerInterface;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Throwable;
20
21
/**
22
 * Class RequestLogger
23
 *
24
 * @package App\Services
25
 * @author TLe, Tarmo Leppänen <[email protected]>
26
 *
27
 * @property  array<int, string> $sensitiveProperties
28
 */
29
class RequestLogger implements RequestLoggerInterface
30
{
31
    private ?Response $response = null;
32
    private ?Request $request = null;
33
    private ?string $userId = null;
34
    private ?string $apiKeyId = null;
35
    private bool $mainRequest = false;
36
37 600
    public function __construct(
38
        private LogRequestResource $logRequestResource,
39
        private UserResource $userResource,
40
        private ApiKeyResource $apiKeyResource,
41
        private LoggerInterface $logger,
42
        private array $sensitiveProperties,
43
    ) {
44 600
    }
45
46 544
    public function setResponse(Response $response): self
47
    {
48 544
        $this->response = $response;
49
50 544
        return $this;
51
    }
52
53 544
    public function setRequest(Request $request): self
54
    {
55 544
        $this->request = $request;
56
57 544
        return $this;
58
    }
59
60 257
    public function setUserId(string $userId): self
61
    {
62 257
        $this->userId = $userId;
63
64 257
        return $this;
65
    }
66
67 6
    public function setApiKeyId(string $apiKeyId): self
68
    {
69 6
        $this->apiKeyId = $apiKeyId;
70
71 6
        return $this;
72
    }
73
74 539
    public function setMainRequest(bool $mainRequest): self
75
    {
76 539
        $this->mainRequest = $mainRequest;
77
78 539
        return $this;
79
    }
80
81 546
    public function handle(): void
82
    {
83
        // Just check that we have all that we need
84 546
        if (!($this->request instanceof Request) || !($this->response instanceof Response)) {
85 3
            return;
86
        }
87
88
        try {
89 543
            $this->createRequestLogEntry();
90 3
        } catch (Throwable $error) {
91 3
            $this->logger->error($error->getMessage());
92
        }
93 543
    }
94
95
    /**
96
     * Store request log to database.
97
     *
98
     * @throws Throwable
99
     */
100 543
    private function createRequestLogEntry(): void
101
    {
102
        /**
103
         * We want to clear possible existing managements entities before we
104
         * flush this new `LogRequest` entity to database. This is to prevent
105
         * not wanted entity state changes to be flushed.
106
         */
107 543
        $this->logRequestResource->getRepository()->getEntityManager()->clear();
108
109 543
        $user = null;
110 543
        $apiKey = null;
111
112 543
        if ($this->userId !== null) {
113 257
            $user = $this->userResource->getReference($this->userId);
114
        }
115
116 543
        if ($this->apiKeyId !== null) {
117 6
            $apiKey = $this->apiKeyResource->getReference($this->apiKeyId);
118
        }
119
120
        // Create new request log entity
121 543
        $entity = new LogRequest(
122 543
            $this->sensitiveProperties,
123 543
            $this->request,
124 543
            $this->response,
125
            $user,
126
            $apiKey,
127 543
            $this->mainRequest
128
        );
129
130 543
        $this->logRequestResource->save($entity, true, true);
131 540
    }
132
}
133