Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetLoanAction
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __invoke
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Loan;
6
7use App\Domain\Exception\NotFoundException;
8use App\Domain\Loan\Service\LoanService;
9use App\Renderer\JsonRenderer;
10use App\Support\Row;
11use Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13
14final readonly class GetLoanAction
15{
16    public function __construct(
17        private LoanService $loanService,
18        private JsonRenderer $renderer
19    ) {}
20
21    /**
22     * @param ServerRequestInterface $request
23     * @param ResponseInterface $response
24     * @param array<string, string> $args
25     * @return ResponseInterface
26     */
27    public function __invoke(
28        ServerRequestInterface $request,
29        ResponseInterface $response,
30        array $args
31    ): ResponseInterface {
32        $loanId = (int)$args['id'];
33        $attributes = $request->getAttributes();
34        $investorId = Row::int($attributes, 'investorId');
35        $isAdmin = Row::nullableBool($attributes, 'isAdmin') ?? false;
36
37        $loan = $this->loanService->getLoan($loanId);
38        if (!$isAdmin && $loan->investorId !== $investorId) {
39            throw new NotFoundException('Loan not found');
40        }
41
42        $data = ['loan' => $loan->toArray()];
43        if ($loan->status === 'active') {
44            $data['paymentSchedule'] = $this->loanService->getPaymentSchedule($loanId);
45        }
46
47        return $this->renderer->json($response, ['success' => true, 'data' => $data]);
48    }
49}