1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of slick/telemetry package |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace Slick\Telemetry\Model; |
||
13 | |||
14 | use Psr\Log\LogLevel; |
||
15 | use Slick\Telemetry\Trackable; |
||
16 | |||
17 | /** |
||
18 | * Request |
||
19 | * |
||
20 | * @package Slick\Telemetry\Model |
||
21 | */ |
||
22 | final class Request implements Trackable |
||
23 | { |
||
24 | use TrackableMethods; |
||
25 | |||
26 | private string $path; |
||
27 | private int $startTime; |
||
28 | private ?int $statusCode; |
||
29 | private float $duration; |
||
30 | |||
31 | /** |
||
32 | * Creates a Request |
||
33 | * |
||
34 | * @param string $message |
||
35 | * @param string $path |
||
36 | * @param int $startTime |
||
37 | * @param int $statusCode |
||
38 | * @param float $duration |
||
39 | * @param iterable|null $context |
||
40 | */ |
||
41 | public function __construct( |
||
42 | string $message, |
||
43 | string $path, |
||
44 | int $startTime, |
||
45 | int $statusCode = 200, |
||
46 | float $duration = 0, |
||
47 | ?iterable $context = [] |
||
48 | ) { |
||
49 | $this->message = $message; |
||
50 | $this->path = $path; |
||
51 | $this->startTime = $startTime; |
||
52 | $this->statusCode = $statusCode; |
||
53 | $this->label = Trackable::LABEL_REQUEST; |
||
54 | $this->duration = $duration; |
||
55 | $this->context = array_merge($context, [ |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | 'label' => $this->label, |
||
57 | 'path' => $path, |
||
58 | 'startTime' => $startTime, |
||
59 | 'statusCode' => $statusCode, |
||
60 | 'duration' => $duration, |
||
61 | 'isSuccessful' => $this->isSuccessful() |
||
62 | ]); |
||
63 | |||
64 | $this->logLevel = $this->isSuccessful() ? $this->logLevel : LogLevel::WARNING; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Requested path |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function path(): string |
||
73 | { |
||
74 | return $this->path; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Request start timestamp |
||
79 | * |
||
80 | * @return int |
||
81 | */ |
||
82 | public function startTime(): int |
||
83 | { |
||
84 | return $this->startTime; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * HTTP request status code |
||
89 | * |
||
90 | * @return int |
||
91 | */ |
||
92 | public function statusCode(): int |
||
93 | { |
||
94 | return $this->statusCode; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * States if the request is a successful one, or not |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | public function isSuccessful(): bool |
||
103 | { |
||
104 | return $this->statusCode < 400; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * duration |
||
109 | * |
||
110 | * @return float|int |
||
111 | */ |
||
112 | public function duration() |
||
113 | { |
||
114 | return $this->duration; |
||
115 | } |
||
116 | } |
||
117 |