Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
7 / 8 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DetailBookController | |
87.50% |
7 / 8 |
|
50.00% |
1 / 2 |
3.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| 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\Library\Presentation\Api; |
| 11 | |
| 12 | use AppDemo\Library\Application\Query\DetailBookQuery; |
| 13 | use AppDemo\Library\Application\Query\DetailBookResponse; |
| 14 | use AppDemo\Library\Presentation\DetailBookJsonPresenter; |
| 15 | use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface; |
| 16 | use Phexium\Plugin\QueryBus\Port\Exception\UnexpectedQueryResponseException; |
| 17 | use Phexium\Plugin\QueryBus\Port\QueryBusInterface; |
| 18 | use Phexium\Presentation\AbstractApiController; |
| 19 | use Phexium\Presentation\ControllerInterface; |
| 20 | use Phexium\Presentation\ResponseBuilderInterface; |
| 21 | use Psr\Http\Message\ResponseInterface; |
| 22 | use Psr\Http\Message\ServerRequestInterface; |
| 23 | |
| 24 | final readonly class DetailBookController extends AbstractApiController implements ControllerInterface |
| 25 | { |
| 26 | public function __construct( |
| 27 | private QueryBusInterface $queryBus, |
| 28 | private DetailBookJsonPresenter $presenter, |
| 29 | ResponseBuilderInterface $responseBuilder, |
| 30 | private IdGeneratorInterface $idGenerator, |
| 31 | ) { |
| 32 | parent::__construct($responseBuilder); |
| 33 | } |
| 34 | |
| 35 | public function index( |
| 36 | ServerRequestInterface $request, // NOSONAR - Slim Framework PSR-15 contract |
| 37 | ResponseInterface $response, |
| 38 | array $args = [] |
| 39 | ): ResponseInterface { |
| 40 | $bookId = $this->idGenerator->from($args['id']); |
| 41 | $query = new DetailBookQuery($bookId); |
| 42 | |
| 43 | $queryResponse = $this->queryBus->dispatch($query); |
| 44 | |
| 45 | if (!$queryResponse instanceof DetailBookResponse) { |
| 46 | throw UnexpectedQueryResponseException::withExpectedType(DetailBookResponse::class, $queryResponse); |
| 47 | } |
| 48 | |
| 49 | $viewModel = $this->presenter->present($queryResponse)->getViewModel(); |
| 50 | |
| 51 | return $this->jsonSuccess($response, $viewModel); |
| 52 | } |
| 53 | } |