Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
28 / 32 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
ActionResponse | |
87.50% |
28 / 32 |
|
71.43% |
5 / 7 |
18.63 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createClientError | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
createCreationSuccess | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
createRunSuccess | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
createRemovedSuccess | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getStatusCode | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
10.37 | |||
getResultAsNativeData | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace Apie\Core\Actions; |
3 | |
4 | use Apie\Core\Context\ApieContext; |
5 | use Apie\Core\Exceptions\ClientRequestException; |
6 | use Apie\Core\Exceptions\HttpStatusCodeException; |
7 | use Throwable; |
8 | |
9 | final class ActionResponse |
10 | { |
11 | public readonly mixed $result; |
12 | |
13 | public readonly mixed $resource; |
14 | |
15 | public readonly Throwable&HttpStatusCodeException $error; |
16 | |
17 | private mixed $nativeData; |
18 | |
19 | public readonly ApieContext $apieContext; |
20 | |
21 | private function __construct(private readonly ApieFacadeInterface $apieFacade, ApieContext $apieContext, public readonly ActionResponseStatus $status) |
22 | { |
23 | $this->apieContext = $apieContext->withContext(ActionResponse::class, $this); |
24 | } |
25 | |
26 | public static function createClientError(ApieFacadeInterface $apieFacade, ApieContext $apieContext, Throwable $error): self |
27 | { |
28 | $res = new self($apieFacade, $apieContext, ActionResponseStatus::CLIENT_ERROR); |
29 | $statusCode = ($error instanceof HttpStatusCodeException) ? $error->getStatusCode() : 500; |
30 | if ($statusCode < 400 || $statusCode >= 500) { |
31 | $error = new ClientRequestException($error); |
32 | } |
33 | $res->result = $error; |
34 | /** @var Throwable&HttpStatusCodeException $error */ |
35 | $res->error = $error; |
36 | return $res; |
37 | } |
38 | |
39 | public static function createCreationSuccess(ApieFacadeInterface $apieFacade, ApieContext $apieContext, mixed $result, mixed $resource): self |
40 | { |
41 | $res = new self($apieFacade, $apieContext, ActionResponseStatus::CREATED); |
42 | $res->result = $result; |
43 | $res->resource = $resource; |
44 | return $res; |
45 | } |
46 | |
47 | public static function createRunSuccess(ApieFacadeInterface $apieFacade, ApieContext $apieContext, mixed $result, mixed $resource): self |
48 | { |
49 | $res = new self($apieFacade, $apieContext, ActionResponseStatus::SUCCESS); |
50 | $res->result = $result; |
51 | $res->resource = $resource; |
52 | return $res; |
53 | } |
54 | |
55 | public static function createRemovedSuccess(ApieFacadeInterface $apieFacade, ApieContext $apieContext): self |
56 | { |
57 | $res = new self($apieFacade, $apieContext, ActionResponseStatus::DELETED); |
58 | $res->result = true; |
59 | $res->resource = true; |
60 | return $res; |
61 | } |
62 | |
63 | /** |
64 | * Returns HTTP status code that should be returned if you create a response. |
65 | */ |
66 | public function getStatusCode(): int |
67 | { |
68 | return match ($this->status) { |
69 | ActionResponseStatus::CLIENT_ERROR => $this->error->getStatusCode(), |
70 | ActionResponseStatus::CREATED => 201, |
71 | ActionResponseStatus::SUCCESS => 200, |
72 | ActionResponseStatus::DELETED => 204, |
73 | ActionResponseStatus::NOT_FOUND => 404, |
74 | ActionResponseStatus::PERISTENCE_ERROR => 409, |
75 | default => 500, |
76 | }; |
77 | } |
78 | |
79 | public function getResultAsNativeData(): mixed |
80 | { |
81 | if (!isset($this->nativeData)) { |
82 | $this->nativeData = $this->apieFacade->normalize($this->result, $this->apieContext); |
83 | } |
84 | return $this->nativeData; |
85 | } |
86 | } |