tarlepp /
symfony-flex-backend
| 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\DBAL\Types\Types; |
||
| 16 | use Doctrine\ORM\Mapping as ORM; |
||
| 17 | use OpenApi\Attributes as OA; |
||
| 18 | use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType; |
||
| 19 | use Ramsey\Uuid\UuidInterface; |
||
| 20 | use Symfony\Component\HttpFoundation\Request; |
||
| 21 | use Symfony\Component\HttpFoundation\Response; |
||
| 22 | use Symfony\Component\Serializer\Annotation\Groups; |
||
| 23 | use Throwable; |
||
| 24 | use function mb_strlen; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Class LogRequest |
||
| 28 | * |
||
| 29 | * @package App\Entity |
||
| 30 | * @author TLe, Tarmo Leppänen <[email protected]> |
||
| 31 | */ |
||
| 32 | #[ORM\Entity( |
||
| 33 | readOnly: true, |
||
| 34 | )] |
||
| 35 | #[ORM\Table( |
||
| 36 | name: 'log_request', |
||
| 37 | )] |
||
| 38 | #[ORM\Index( |
||
| 39 | columns: [ |
||
| 40 | 'user_id', |
||
| 41 | ], |
||
| 42 | name: 'user_id', |
||
| 43 | )] |
||
| 44 | #[ORM\Index( |
||
| 45 | columns: [ |
||
| 46 | 'api_key_id', |
||
| 47 | ], |
||
| 48 | name: 'api_key_id', |
||
| 49 | )] |
||
| 50 | #[ORM\Index( |
||
| 51 | columns: [ |
||
| 52 | 'date', |
||
| 53 | ], |
||
| 54 | name: 'request_date', |
||
| 55 | )] |
||
| 56 | #[ORM\HasLifecycleCallbacks] |
||
| 57 | #[ORM\ChangeTrackingPolicy('DEFERRED_EXPLICIT')] |
||
| 58 | class LogRequest implements EntityInterface |
||
| 59 | { |
||
| 60 | use LogEntityTrait; |
||
| 61 | use LogRequestProcessRequestTrait; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 62 | use Uuid; |
||
| 63 | |||
| 64 | #[ORM\Id] |
||
| 65 | #[ORM\Column( |
||
| 66 | name: 'id', |
||
| 67 | type: UuidBinaryOrderedTimeType::NAME, |
||
| 68 | unique: true, |
||
| 69 | )] |
||
| 70 | #[Groups([ |
||
| 71 | 'LogRequest', |
||
| 72 | 'LogRequest.id', |
||
| 73 | |||
| 74 | 'ApiKey.logsRequest', |
||
| 75 | 'User.logsRequest', |
||
| 76 | ])] |
||
| 77 | #[OA\Property(type: 'string', format: 'uuid')] |
||
| 78 | private UuidInterface $id; |
||
| 79 | |||
| 80 | #[ORM\Column( |
||
| 81 | name: 'status_code', |
||
| 82 | type: Types::INTEGER, |
||
| 83 | )] |
||
| 84 | #[Groups([ |
||
| 85 | 'LogRequest', |
||
| 86 | 'LogRequest.statusCode', |
||
| 87 | ])] |
||
| 88 | private int $statusCode = 0; |
||
| 89 | |||
| 90 | #[ORM\Column( |
||
| 91 | name: 'response_content_length', |
||
| 92 | type: Types::INTEGER, |
||
| 93 | )] |
||
| 94 | #[Groups([ |
||
| 95 | 'LogRequest', |
||
| 96 | 'LogRequest.responseContentLength', |
||
| 97 | ])] |
||
| 98 | private int $responseContentLength = 0; |
||
| 99 | |||
| 100 | #[ORM\Column( |
||
| 101 | name: 'is_main_request', |
||
| 102 | type: Types::BOOLEAN, |
||
| 103 | )] |
||
| 104 | #[Groups([ |
||
| 105 | 'LogRequest', |
||
| 106 | 'LogRequest.isMainRequest', |
||
| 107 | ])] |
||
| 108 | private bool $mainRequest; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * LogRequest constructor. |
||
| 112 | * |
||
| 113 | * @param array<int, string> $sensitiveProperties |
||
| 114 | * |
||
| 115 | * @throws Throwable |
||
| 116 | */ |
||
| 117 | 615 | public function __construct( |
|
| 118 | private array $sensitiveProperties, |
||
| 119 | ?Request $request = null, |
||
| 120 | ?Response $response = null, |
||
| 121 | #[ORM\ManyToOne( |
||
| 122 | targetEntity: User::class, |
||
| 123 | inversedBy: 'logsRequest', |
||
| 124 | )] |
||
| 125 | #[ORM\JoinColumn( |
||
| 126 | name: 'user_id', |
||
| 127 | onDelete: 'SET NULL', |
||
| 128 | )] |
||
| 129 | #[Groups([ |
||
| 130 | 'LogRequest.user', |
||
| 131 | ])] |
||
| 132 | private ?User $user = null, |
||
| 133 | #[ORM\ManyToOne( |
||
| 134 | targetEntity: ApiKey::class, |
||
| 135 | inversedBy: 'logsRequest', |
||
| 136 | )] |
||
| 137 | #[ORM\JoinColumn( |
||
| 138 | name: 'api_key_id', |
||
| 139 | onDelete: 'SET NULL', |
||
| 140 | )] |
||
| 141 | #[Groups([ |
||
| 142 | 'LogRequest.apiKey', |
||
| 143 | ])] |
||
| 144 | private ?ApiKey $apiKey = null, |
||
| 145 | ?bool $mainRequest = null |
||
| 146 | ) { |
||
| 147 | 615 | $this->id = $this->createUuid(); |
|
| 148 | 615 | $this->mainRequest = $mainRequest ?? true; |
|
| 149 | |||
| 150 | 615 | $this->processTimeAndDate(); |
|
| 151 | |||
| 152 | 615 | if ($request !== null) { |
|
| 153 | 583 | $this->processRequestData($request); |
|
| 154 | 583 | $this->processRequest($request); |
|
| 155 | } |
||
| 156 | |||
| 157 | 615 | if ($response !== null) { |
|
| 158 | 583 | $this->processResponse($response); |
|
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | 2 | public function getId(): string |
|
| 163 | { |
||
| 164 | 2 | return $this->id->toString(); |
|
| 165 | } |
||
| 166 | |||
| 167 | 3 | public function getUser(): ?User |
|
| 168 | { |
||
| 169 | 3 | return $this->user; |
|
| 170 | } |
||
| 171 | |||
| 172 | 1 | public function getStatusCode(): int |
|
| 173 | { |
||
| 174 | 1 | return $this->statusCode; |
|
| 175 | } |
||
| 176 | |||
| 177 | 1 | public function getResponseContentLength(): int |
|
| 178 | { |
||
| 179 | 1 | return $this->responseContentLength; |
|
| 180 | } |
||
| 181 | |||
| 182 | 1 | public function getApiKey(): ?ApiKey |
|
| 183 | { |
||
| 184 | 1 | return $this->apiKey; |
|
| 185 | } |
||
| 186 | |||
| 187 | 1 | public function isMainRequest(): bool |
|
| 188 | { |
||
| 189 | 1 | return $this->mainRequest; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return array<int, string> |
||
| 194 | */ |
||
| 195 | 583 | public function getSensitiveProperties(): array |
|
| 196 | { |
||
| 197 | 583 | return $this->sensitiveProperties; |
|
| 198 | } |
||
| 199 | |||
| 200 | 583 | private function processResponse(Response $response): void |
|
| 201 | { |
||
| 202 | 583 | $content = $response->getContent(); |
|
| 203 | |||
| 204 | 583 | $this->statusCode = $response->getStatusCode(); |
|
| 205 | 583 | $this->responseContentLength = $content === false ? 0 : mb_strlen($content); |
|
|
0 ignored issues
–
show
|
|||
| 206 | } |
||
| 207 | } |
||
| 208 |