Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ListBooksController | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
3.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| 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 AppDemo\Library\Presentation\Api; |
| 11 | |
| 12 | use AppDemo\Library\Application\Query\ListBooksQuery; |
| 13 | use AppDemo\Library\Application\Query\ListBooksResponse; |
| 14 | use AppDemo\Library\Presentation\ListBooksJsonPresenter; |
| 15 | use Phexium\Plugin\QueryBus\Port\Exception\UnexpectedQueryResponseException; |
| 16 | use Phexium\Plugin\QueryBus\Port\QueryBusInterface; |
| 17 | use Phexium\Presentation\AbstractApiController; |
| 18 | use Phexium\Presentation\ControllerInterface; |
| 19 | use Phexium\Presentation\ResponseBuilderInterface; |
| 20 | use Psr\Http\Message\ResponseInterface; |
| 21 | use Psr\Http\Message\ServerRequestInterface; |
| 22 | |
| 23 | final readonly class ListBooksController extends AbstractApiController implements ControllerInterface |
| 24 | { |
| 25 | public function __construct( |
| 26 | private QueryBusInterface $queryBus, |
| 27 | private ListBooksJsonPresenter $presenter, |
| 28 | ResponseBuilderInterface $responseBuilder, |
| 29 | ) { |
| 30 | parent::__construct($responseBuilder); |
| 31 | } |
| 32 | |
| 33 | public function index( |
| 34 | ServerRequestInterface $request, |
| 35 | ResponseInterface $response |
| 36 | ): ResponseInterface { |
| 37 | $page = max(1, (int) ($request->getQueryParams()['page'] ?? 1)); |
| 38 | |
| 39 | $query = new ListBooksQuery(page: $page); |
| 40 | |
| 41 | $queryResponse = $this->queryBus->dispatch($query); |
| 42 | |
| 43 | if (!$queryResponse instanceof ListBooksResponse) { |
| 44 | throw UnexpectedQueryResponseException::withExpectedType(ListBooksResponse::class, $queryResponse); |
| 45 | } |
| 46 | |
| 47 | $viewModel = $this->presenter |
| 48 | ->present($queryResponse) |
| 49 | ->getViewModel() |
| 50 | ; |
| 51 | |
| 52 | return $this->jsonSuccess($response, $viewModel); |
| 53 | } |
| 54 | } |