Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| AbstractApiController | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| jsonSuccess | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| jsonCreated | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| jsonError | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Phexium\Application; |
| 11 | |
| 12 | use Phexium\Presentation\ResponseBuilderInterface; |
| 13 | use Psr\Http\Message\ResponseInterface; |
| 14 | |
| 15 | abstract readonly class AbstractApiController extends AbstractController |
| 16 | { |
| 17 | protected ResponseBuilderInterface $responseBuilder; |
| 18 | |
| 19 | protected function jsonSuccess(ResponseInterface $response, array $payload, int $statusCode = 200): ResponseInterface |
| 20 | { |
| 21 | return $this->responseBuilder |
| 22 | ->withResponse($response) |
| 23 | ->withStatus($statusCode) |
| 24 | ->withJson($payload) |
| 25 | ->build() |
| 26 | ; |
| 27 | } |
| 28 | |
| 29 | protected function jsonCreated(ResponseInterface $response): ResponseInterface |
| 30 | { |
| 31 | return $this->responseBuilder |
| 32 | ->withResponse($response) |
| 33 | ->withStatus(201) |
| 34 | ->build() |
| 35 | ; |
| 36 | } |
| 37 | |
| 38 | protected function jsonError(ResponseInterface $response, int $statusCode, string $message): ResponseInterface |
| 39 | { |
| 40 | return $this->responseBuilder |
| 41 | ->withResponse($response) |
| 42 | ->withStatus($statusCode) |
| 43 | ->withJson(['error' => $message]) |
| 44 | ->build() |
| 45 | ; |
| 46 | } |
| 47 | } |