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