Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ListBooksHandler
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
2
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
 __invoke
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
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\Book;
13use AppDemo\Library\Domain\BookRepository;
14use Phexium\Application\Query\QueryHandlerInterface;
15use Phexium\Domain\Pagination;
16use Phexium\Domain\Specification\AlwaysTrueSpecification;
17
18final readonly class ListBooksHandler implements QueryHandlerInterface
19{
20    public function __construct(
21        private BookRepository $bookRepository
22    ) {}
23
24    public function __invoke(ListBooksQuery $query): ListBooksResponse
25    {
26        $specification = new AlwaysTrueSpecification();
27        $pagination = new Pagination(
28            totalCount: $this->bookRepository->countBy($specification),
29            page: $query->page,
30            perPage: $query->perPage,
31        );
32
33        $books = $this->bookRepository
34            ->findBy($specification, null, $pagination->getOffset(), $query->perPage)
35            ->mapToArray(fn (Book $book): array => [
36                'id' => $book->getId()->getValue(),
37                'title' => $book->getTitle()->getValue(),
38                'author' => $book->getAuthor()->getValue(),
39                'isbn' => $book->getIsbn()->getFormattedValue(),
40                'status' => $book->getStatus()->value,
41            ])
42        ;
43
44        return new ListBooksResponse($books, $pagination);
45    }
46}