Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
BorrowBookHandler
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
4
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%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
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\Loan\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 BorrowBookHandler 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(BorrowBookQuery::class);
30
31        $allBooks = $this->bookRepository->findAll();
32        $availableBooks = [];
33
34        foreach ($allBooks as $book) {
35            if ($book->getStatus()->isAvailable()) {
36                $availableBooks[] = [
37                    'id' => $book->getId()->getValue(),
38                    'title' => $book->getTitle()->getValue(),
39                    'author' => $book->getAuthor()->getValue(),
40                    'isbn' => $book->getIsbn()->getValue(),
41                ];
42            }
43        }
44
45        return new BorrowBookResponse(
46            $availableBooks,
47            $query->formValues,
48            $query->errors
49        );
50    }
51}