Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListLoansController
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
3.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 index
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
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
8declare(strict_types=1);
9
10namespace AppDemo\Loan\Presentation\Http;
11
12use AppDemo\Loan\Application\Query\ListLoansQuery;
13use AppDemo\Loan\Application\Query\ListLoansResponse;
14use AppDemo\Loan\Presentation\ListLoansHtmlPresenter;
15use AppDemo\Shared\Presentation\AbstractHttpController;
16use Phexium\Plugin\QueryBus\Port\Exception\UnexpectedQueryResponseException;
17use Phexium\Plugin\QueryBus\Port\QueryBusInterface;
18use Phexium\Presentation\ControllerInterface;
19use Phexium\Presentation\ResponseBuilderInterface;
20use Psr\Http\Message\ResponseInterface;
21use Psr\Http\Message\ServerRequestInterface;
22use Twig\Environment;
23
24final 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}