Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
14 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MyLoansController
87.50% covered (warning)
87.50%
14 / 16
50.00% covered (danger)
50.00%
1 / 2
4.03
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
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
3.02
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\MyLoansQuery;
13use AppDemo\Loan\Application\Query\MyLoansResponse;
14use AppDemo\Loan\Presentation\MyLoansHtmlPresenter;
15use AppDemo\Shared\Presentation\AbstractHttpController;
16use Phexium\Domain\Id\IdInterface;
17use Phexium\Plugin\QueryBus\Port\Exception\UnexpectedQueryResponseException;
18use Phexium\Plugin\QueryBus\Port\QueryBusInterface;
19use Phexium\Presentation\ControllerInterface;
20use Phexium\Presentation\ResponseBuilderInterface;
21use Psr\Http\Message\ResponseInterface;
22use Psr\Http\Message\ServerRequestInterface;
23use RuntimeException;
24use Twig\Environment;
25
26final readonly class MyLoansController extends AbstractHttpController implements ControllerInterface
27{
28    public function __construct(
29        private QueryBusInterface $queryBus,
30        private MyLoansHtmlPresenter $presenter,
31        private Environment $twig,
32        private ResponseBuilderInterface $responseBuilder,
33    ) {}
34
35    public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
36    {
37        $userId = $this->getUserId($request);
38
39        if (!$userId instanceof IdInterface) {
40            throw new RuntimeException('No authenticated user found in session.');
41        }
42
43        $query = new MyLoansQuery($userId);
44
45        $queryResponse = $this->queryBus->dispatch($query);
46
47        if (!$queryResponse instanceof MyLoansResponse) {
48            throw UnexpectedQueryResponseException::withExpectedType(MyLoansResponse::class, $queryResponse);
49        }
50
51        $viewModel = $this->presenter->present($queryResponse)->getViewModel();
52        $html = $this->twig->render('MyLoans.html.twig', (array) $viewModel);
53
54        return $this->responseBuilder
55            ->withResponse($response)
56            ->withStatus(200)
57            ->withHtml($html)
58            ->build()
59        ;
60    }
61}