Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
BorrowBookHandler
100.00% covered (success)
100.00%
16 / 16
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%
15 / 15
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\Loan\Application\Query;
11
12use AppDemo\Library\Domain\Book;
13use AppDemo\Library\Domain\BookRepository;
14use Phexium\Application\Query\QueryHandlerInterface;
15
16final readonly class BorrowBookHandler implements QueryHandlerInterface
17{
18    public function __construct(
19        private BookRepository $bookRepository
20    ) {}
21
22    public function __invoke(BorrowBookQuery $query): BorrowBookResponse
23    {
24        $availableBooks = $this->bookRepository
25            ->findAll()
26            ->filter(fn (Book $book): bool => $book->getStatus()->isAvailable())
27            ->mapToArray(fn (Book $book): array => [
28                'id' => $book->getId()->getValue(),
29                'title' => $book->getTitle()->getValue(),
30                'author' => $book->getAuthor()->getValue(),
31                'isbn' => $book->getIsbn()->getValue(),
32            ])
33        ;
34
35        return new BorrowBookResponse(
36            $availableBooks,
37            $query->formValues,
38            $query->errors
39        );
40    }
41}