Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListBooksController
94.74% covered (success)
94.74%
18 / 19
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
94.44% covered (success)
94.44%
17 / 18
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\Library\Presentation\Http;
11
12use AppDemo\Library\Application\Query\ListBooksQuery;
13use AppDemo\Library\Application\Query\ListBooksResponse;
14use AppDemo\Library\Presentation\ListBooksHtmlPresenter;
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 ListBooksController extends AbstractHttpController implements ControllerInterface
25{
26    public function __construct(
27        private QueryBusInterface $queryBus,
28        private ListBooksHtmlPresenter $presenter,
29        private Environment $twig,
30        private ResponseBuilderInterface $responseBuilder,
31    ) {}
32
33    public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
34    {
35        $page = max(1, (int) ($request->getQueryParams()['page'] ?? 1));
36
37        $query = new ListBooksQuery(page: $page);
38
39        $queryResponse = $this->queryBus->dispatch($query);
40
41        if (!$queryResponse instanceof ListBooksResponse) {
42            throw UnexpectedQueryResponseException::withExpectedType(ListBooksResponse::class, $queryResponse);
43        }
44
45        $context = $this->createPresentationContext($request);
46
47        $viewModel = $this->presenter
48            ->withContext($context)
49            ->present($queryResponse)
50            ->getViewModel()
51        ;
52
53        $html = $this->twig->render('ListBooks.html.twig', (array) $viewModel);
54
55        return $this->responseBuilder
56            ->withResponse($response)
57            ->withStatus(200)
58            ->withHtml($html)
59            ->build()
60        ;
61    }
62}