Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ListLoansHandler
100.00% covered (success)
100.00%
23 / 23
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%
22 / 22
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 Phexium\Application\Query\QueryHandlerInterface;
15use Phexium\Domain\Pagination;
16
17final readonly class ListLoansHandler implements QueryHandlerInterface
18{
19    public function __construct(
20        private LoanRepository $loanRepository,
21    ) {}
22
23    public function __invoke(ListLoansQuery $query): ListLoansResponse
24    {
25        $pagination = new Pagination(
26            totalCount: $this->loanRepository->countAllWithDetails(),
27            page: $query->page,
28            perPage: $query->perPage,
29        );
30
31        $loansWithDetails = $this->loanRepository->findAllWithDetails($pagination->getOffset(), $query->perPage);
32
33        $loanItems = array_map(
34            fn (LoanWithDetails $loan): array => [
35                'id' => $loan->loanId,
36                'userId' => $loan->userId,
37                'bookId' => $loan->bookId,
38                'bookTitle' => $loan->bookTitle,
39                'userEmail' => $loan->userEmail,
40                'borrowedAt' => $loan->borrowedAt,
41                'dueAt' => $loan->dueAt,
42                'returnedAt' => $loan->returnedAt,
43                'status' => $loan->status,
44                'isOverdue' => $loan->isOverdue,
45            ],
46            $loansWithDetails
47        );
48
49        return new ListLoansResponse($loanItems, $pagination);
50    }
51}