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

RequestLogger   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 82
ccs 33
cts 33
cp 1
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setApiKey() 0 5 1
A handle() 0 11 4
A setResponse() 0 5 1
A setUser() 0 5 1
A createRequestLogEntry() 0 13 1
A __construct() 0 5 1
A setRequest() 0 5 1
A setMainRequest() 0 5 1
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\ApiKey;
12
use App\Entity\LogRequest;
13
use App\Entity\User;
14
use App\Resource\LogRequestResource;
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 ?User $user = null;
34
    private ?ApiKey $apiKey = null;
35
    private bool $mainRequest = false;
36
37 481
    public function __construct(
38
        private LogRequestResource $resource,
39
        private LoggerInterface $logger,
40
        private array $sensitiveProperties,
41
    ) {
42 481
    }
43
44 445
    public function setResponse(Response $response): self
45
    {
46 445
        $this->response = $response;
47
48 445
        return $this;
49
    }
50
51 445
    public function setRequest(Request $request): self
52
    {
53 445
        $this->request = $request;
54
55 445
        return $this;
56
    }
57
58 247
    public function setUser(?User $user = null): self
59
    {
60 247
        $this->user = $user;
61
62 247
        return $this;
63
    }
64
65 20
    public function setApiKey(?ApiKey $apiKey = null): self
66
    {
67 20
        $this->apiKey = $apiKey;
68
69 20
        return $this;
70
    }
71
72 442
    public function setMainRequest(bool $mainRequest): self
73
    {
74 442
        $this->mainRequest = $mainRequest;
75
76 442
        return $this;
77
    }
78
79 447
    public function handle(): void
80
    {
81
        // Just check that we have all that we need
82 447
        if (!($this->request instanceof Request) || !($this->response instanceof Response)) {
83 3
            return;
84
        }
85
86
        try {
87 444
            $this->createRequestLogEntry();
88 1
        } catch (Throwable $error) {
89 1
            $this->logger->error($error->getMessage());
90
        }
91 444
    }
92
93
    /**
94
     * Store request log to database.
95
     *
96
     * @throws Throwable
97
     */
98 444
    private function createRequestLogEntry(): void
99
    {
100
        // Create new request log entity
101 444
        $entity = new LogRequest(
102 444
            $this->sensitiveProperties,
103 444
            $this->request,
104 444
            $this->response,
105 444
            $this->user,
106 444
            $this->apiKey,
107 444
            $this->mainRequest
108
        );
109
110 444
        $this->resource->save($entity, true, true);
111 443
    }
112
}
113