Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ListLoansController | |
94.12% |
16 / 17 |
|
50.00% |
1 / 2 |
3.00 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
93.75% |
15 / 16 |
|
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\Loan\Presentation\Http; |
| 11 | |
| 12 | use AppDemo\Loan\Application\Query\ListLoansQuery; |
| 13 | use AppDemo\Loan\Application\Query\ListLoansResponse; |
| 14 | use AppDemo\Loan\Presentation\ListLoansHtmlPresenter; |
| 15 | use AppDemo\Shared\Presentation\AbstractHttpController; |
| 16 | use Phexium\Plugin\QueryBus\Port\Exception\UnexpectedQueryResponseException; |
| 17 | use Phexium\Plugin\QueryBus\Port\QueryBusInterface; |
| 18 | use Phexium\Presentation\ControllerInterface; |
| 19 | use Phexium\Presentation\ResponseBuilderInterface; |
| 20 | use Psr\Http\Message\ResponseInterface; |
| 21 | use Psr\Http\Message\ServerRequestInterface; |
| 22 | use Twig\Environment; |
| 23 | |
| 24 | final readonly class ListLoansController extends AbstractHttpController implements ControllerInterface |
| 25 | { |
| 26 | public function __construct( |
| 27 | private QueryBusInterface $queryBus, |
| 28 | private ListLoansHtmlPresenter $presenter, |
| 29 | private Environment $twig, |
| 30 | private ResponseBuilderInterface $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 ListLoansQuery(page: $page); |
| 40 | |
| 41 | $queryResponse = $this->queryBus->dispatch($query); |
| 42 | |
| 43 | if (!$queryResponse instanceof ListLoansResponse) { |
| 44 | throw UnexpectedQueryResponseException::withExpectedType(ListLoansResponse::class, $queryResponse); |
| 45 | } |
| 46 | |
| 47 | $viewModel = $this->presenter |
| 48 | ->present($queryResponse) |
| 49 | ->getViewModel() |
| 50 | ; |
| 51 | |
| 52 | $html = $this->twig->render('ListLoans.html.twig', (array) $viewModel); |
| 53 | |
| 54 | return $this->responseBuilder |
| 55 | ->withResponse($response) |
| 56 | ->withStatus(200) |
| 57 | ->withHtml($html) |
| 58 | ->build() |
| 59 | ; |
| 60 | } |
| 61 | } |