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
DetailBookController
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\DetailBookQuery;
13use AppDemo\Library\Application\Query\DetailBookResponse;
14use AppDemo\Library\Presentation\DetailBookHtmlPresenter;
15use AppDemo\Shared\Presentation\AbstractHttpController;
16use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
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 Twig\Environment;
24
25final readonly class DetailBookController extends AbstractHttpController implements ControllerInterface
26{
27    public function __construct(
28        private QueryBusInterface $queryBus,
29        private DetailBookHtmlPresenter $presenter,
30        private Environment $twig,
31        private ResponseBuilderInterface $responseBuilder,
32        private IdGeneratorInterface $idGenerator,
33    ) {}
34
35    public function index(ServerRequestInterface $request, ResponseInterface $response, array $args = []): ResponseInterface
36    {
37        $bookId = $this->idGenerator->from($args['id']);
38        $query = new DetailBookQuery($bookId);
39
40        $queryResponse = $this->queryBus->dispatch($query);
41
42        if (!$queryResponse instanceof DetailBookResponse) {
43            throw UnexpectedQueryResponseException::withExpectedType(DetailBookResponse::class, $queryResponse);
44        }
45
46        $context = $this->createPresentationContext($request);
47
48        $viewModel = $this->presenter
49            ->withContext($context)
50            ->present($queryResponse)
51            ->getViewModel()
52        ;
53
54        $html = $this->twig->render('DetailBook.html.twig', (array) $viewModel);
55
56        return $this->responseBuilder
57            ->withResponse($response)
58            ->withStatus(200)
59            ->withHtml($html)
60            ->build()
61        ;
62    }
63}