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
MyLoansHandler
100.00% covered (success)
100.00%
17 / 17
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
 handle
100.00% covered (success)
100.00%
16 / 16
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\Loan\Domain\LoanRepository;
13use AppDemo\Loan\Domain\ReadModel\LoanWithDetails;
14use Override;
15use Phexium\Application\Query\AbstractQueryHandler;
16use Phexium\Application\Query\QueryHandlerInterface;
17use Phexium\Application\Query\QueryInterface;
18use Phexium\Application\Query\QueryResponseInterface;
19use Phexium\Domain\TypeGuard;
20
21final class MyLoansHandler 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(MyLoansQuery::class);
31
32        $loansWithDetails = $this->loanRepository->findByUserIdWithBookDetails($query->userId);
33
34        $loanItems = array_map(
35            fn (LoanWithDetails $loan): array => [
36                'id' => $loan->loanId,
37                'bookId' => $loan->bookId,
38                'bookTitle' => $loan->bookTitle,
39                'borrowedAt' => $loan->borrowedAt,
40                'dueAt' => $loan->dueAt,
41                'returnedAt' => $loan->returnedAt,
42                'status' => $loan->status,
43                'isOverdue' => $loan->isOverdue,
44            ],
45            $loansWithDetails
46        );
47
48        return new MyLoansResponse($loanItems);
49    }
50}