Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ListBooksHandler
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
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\Application\Query;
11
12use AppDemo\Library\Domain\BookRepository;
13use Override;
14use Phexium\Application\Query\AbstractQueryHandler;
15use Phexium\Application\Query\QueryHandlerInterface;
16use Phexium\Application\Query\QueryInterface;
17use Phexium\Application\Query\QueryResponseInterface;
18use Phexium\Domain\TypeGuard;
19
20final class ListBooksHandler extends AbstractQueryHandler implements QueryHandlerInterface
21{
22    public function __construct(
23        private readonly BookRepository $bookRepository
24    ) {}
25
26    #[Override]
27    public function handle(QueryInterface $query): QueryResponseInterface
28    {
29        TypeGuard::that($query)->isInstanceOf(ListBooksQuery::class);
30
31        $books = $this->bookRepository->findAll();
32
33        $bookItems = [];
34
35        foreach ($books as $book) {
36            $bookItems[] = [
37                'id' => $book->getId()->getValue(),
38                'title' => $book->getTitle()->getValue(),
39                'author' => $book->getAuthor()->getValue(),
40                'isbn' => $book->getIsbn()->getFormattedValue(),
41                'status' => $book->getStatus()->value,
42            ];
43        }
44
45        return new ListBooksResponse($bookItems);
46    }
47}