Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ListLoansHandler | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
18 / 18 |
|
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 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace AppDemo\Loan\Application\Query; |
| 11 | |
| 12 | use AppDemo\Loan\Domain\LoanRepository; |
| 13 | use AppDemo\Loan\Domain\ReadModel\LoanWithDetails; |
| 14 | use Override; |
| 15 | use Phexium\Application\Query\AbstractQueryHandler; |
| 16 | use Phexium\Application\Query\QueryHandlerInterface; |
| 17 | use Phexium\Application\Query\QueryInterface; |
| 18 | use Phexium\Application\Query\QueryResponseInterface; |
| 19 | use Phexium\Domain\TypeGuard; |
| 20 | |
| 21 | final class ListLoansHandler extends AbstractQueryHandler implements QueryHandlerInterface |
| 22 | { |
| 23 | public function __construct( |
| 24 | private readonly LoanRepository $loanRepository, |
| 25 | ) {} |
| 26 | |
| 27 | #[Override] |
| 28 | public function handle(QueryInterface $query): QueryResponseInterface |
| 29 | { |
| 30 | TypeGuard::that($query)->isInstanceOf(ListLoansQuery::class); |
| 31 | |
| 32 | $loansWithDetails = $this->loanRepository->findAllWithDetails(); |
| 33 | |
| 34 | $loanItems = array_map( |
| 35 | fn (LoanWithDetails $loan): array => [ |
| 36 | 'id' => $loan->loanId, |
| 37 | 'userId' => $loan->userId, |
| 38 | 'bookId' => $loan->bookId, |
| 39 | 'bookTitle' => $loan->bookTitle, |
| 40 | 'userEmail' => $loan->userEmail, |
| 41 | 'borrowedAt' => $loan->borrowedAt, |
| 42 | 'dueAt' => $loan->dueAt, |
| 43 | 'returnedAt' => $loan->returnedAt, |
| 44 | 'status' => $loan->status, |
| 45 | 'isOverdue' => $loan->isOverdue, |
| 46 | ], |
| 47 | $loansWithDetails |
| 48 | ); |
| 49 | |
| 50 | return new ListLoansResponse($loanItems); |
| 51 | } |
| 52 | } |