Completed
Push — master ( 126c81...e32e37 )
by Tarmo
19s queued 13s
created

LogRequest::getSensitiveProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Entity/LogRequest.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Entity;
10
11
use App\Entity\Interfaces\EntityInterface;
12
use App\Entity\Traits\LogEntityTrait;
13
use App\Entity\Traits\LogRequestProcessRequestTrait;
14
use App\Entity\Traits\Uuid;
15
use Doctrine\ORM\Mapping as ORM;
16
use OpenApi\Annotations as OA;
17
use Ramsey\Uuid\UuidInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Serializer\Annotation\Groups;
21
use Throwable;
22
use function mb_strlen;
23
24
/**
25
 * Class LogRequest
26
 *
27
 * @ORM\Table(
28
 *      name="log_request",
29
 *      indexes={
30
 *          @ORM\Index(name="user_id", columns={"user_id"}),
31
 *          @ORM\Index(name="api_key_id", columns={"api_key_id"}),
32
 *          @ORM\Index(name="request_date", columns={"date"}),
33
 *      },
34
 *  )
35
 * @ORM\Entity(
36
 *      readOnly=true,
37
 *  )
38
 * @ORM\HasLifecycleCallbacks()
39
 *
40
 * @package App\Entity
41
 * @author TLe, Tarmo Leppänen <[email protected]>
42
 */
43
class LogRequest implements EntityInterface
44
{
45
    use LogEntityTrait;
46
    use LogRequestProcessRequestTrait;
0 ignored issues
show
Bug introduced by
The trait App\Entity\Traits\LogRequestProcessRequestTrait requires the property $request which is not provided by App\Entity\LogRequest.
Loading history...
47
    use Uuid;
48
49
    /**
50
     * @ORM\Column(
51
     *      name="id",
52
     *      type="uuid_binary_ordered_time",
53
     *      unique=true,
54
     *      nullable=false,
55
     *  )
56
     * @ORM\Id()
57
     *
58
     * @OA\Property(type="string", format="uuid")
59
     */
60
    #[Groups([
61
        'LogRequest',
62
        'LogRequest.id',
63
64
        'ApiKey.logsRequest',
65
        'User.logsRequest',
66
    ])]
67
    private UuidInterface $id;
68
69
    /**
70
     * @ORM\ManyToOne(
71
     *      targetEntity="App\Entity\User",
72
     *      inversedBy="logsRequest",
73
     *  )
74
     * @ORM\JoinColumns({
75
     *      @ORM\JoinColumn(
76
     *          name="user_id",
77
     *          referencedColumnName="id",
78
     *          nullable=true,
79
     *          onDelete="SET NULL",
80
     *      ),
81
     *  })
82
     */
83
    #[Groups([
84
        'LogRequest.user',
85
    ])]
86
    private ?User $user;
87
88
    /**
89
     * @ORM\ManyToOne(
90
     *      targetEntity="App\Entity\ApiKey",
91
     *      inversedBy="logsRequest",
92
     *  )
93
     * @ORM\JoinColumns({
94
     *      @ORM\JoinColumn(
95
     *          name="api_key_id",
96
     *          referencedColumnName="id",
97
     *          nullable=true,
98
     *          onDelete="SET NULL",
99
     *      ),
100
     *  })
101
     */
102
    #[Groups([
103
        'LogRequest.apiKey',
104
    ])]
105
    private ?ApiKey $apiKey;
106
107
    /**
108
     * @ORM\Column(
109
     *      name="status_code",
110
     *      type="integer",
111
     *      nullable=false,
112
     *  )
113
     */
114
    #[Groups([
115
        'LogRequest',
116
        'LogRequest.statusCode',
117
    ])]
118
    private int $statusCode = 0;
119
120
    /**
121
     * @ORM\Column(
122
     *      name="response_content_length",
123
     *      type="integer",
124
     *      nullable=false,
125
     *  )
126
     */
127
    #[Groups([
128
        'LogRequest',
129
        'LogRequest.responseContentLength',
130
    ])]
131
    private int $responseContentLength = 0;
132
133
    /**
134
     * @ORM\Column(
135
     *      name="is_main_request",
136
     *      type="boolean",
137
     *      nullable=false,
138
     *  )
139
     */
140
    #[Groups([
141
        'LogRequest',
142
        'LogRequest.isMainRequest',
143
    ])]
144
    private bool $mainRequest;
145
146
    /**
147
     * LogRequest constructor.
148
     *
149
     * @param array<int, string> $sensitiveProperties
150
     *
151
     * @throws Throwable
152
     */
153 513
    public function __construct(
154
        private array $sensitiveProperties,
155
        ?Request $request = null,
156
        ?Response $response = null,
157
        ?User $user = null,
158
        ?ApiKey $apiKey = null,
159
        ?bool $mainRequest = null
160
    ) {
161 513
        $this->id = $this->createUuid();
162 513
        $this->user = $user;
163 513
        $this->apiKey = $apiKey;
164 513
        $this->mainRequest = $mainRequest ?? true;
165
166 513
        $this->processTimeAndDate();
167
168 513
        if ($request !== null) {
169 481
            $this->processRequestData($request);
170 481
            $this->processRequest($request);
171
        }
172
173 513
        if ($response !== null) {
174 481
            $this->processResponse($response);
175
        }
176 513
    }
177
178 2
    public function getId(): string
179
    {
180 2
        return $this->id->toString();
181
    }
182
183 3
    public function getUser(): ?User
184
    {
185 3
        return $this->user;
186
    }
187
188 1
    public function getStatusCode(): int
189
    {
190 1
        return $this->statusCode;
191
    }
192
193 1
    public function getResponseContentLength(): int
194
    {
195 1
        return $this->responseContentLength;
196
    }
197
198 1
    public function getApiKey(): ?ApiKey
199
    {
200 1
        return $this->apiKey;
201
    }
202
203 1
    public function isMainRequest(): bool
204
    {
205 1
        return $this->mainRequest;
206
    }
207
208
    /**
209
     * @return array<int, string>
210
     */
211 481
    public function getSensitiveProperties(): array
212
    {
213 481
        return $this->sensitiveProperties;
214
    }
215
216 481
    private function processResponse(Response $response): void
217
    {
218 481
        $content = $response->getContent();
219
220 481
        $this->statusCode = $response->getStatusCode();
221 481
        $this->responseContentLength = $content === false ? 0 : mb_strlen($content);
0 ignored issues
show
introduced by
The condition $content === false is always false.
Loading history...
222 481
    }
223
}
224