Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
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 / 11
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 / 10
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 Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13final readonly class GetLoanAction
14{
15    public function __construct(
16        private LoanService $loanService,
17        private JsonRenderer $renderer
18    ) {}
19
20    /**
21     * @param ServerRequestInterface $request
22     * @param ResponseInterface $response
23     * @param array<string, string> $args
24     * @return ResponseInterface
25     */
26    public function __invoke(
27        ServerRequestInterface $request,
28        ResponseInterface $response,
29        array $args
30    ): ResponseInterface {
31        $loanId = (int)$args['id'];
32        $investorId = (int)$request->getAttribute('investorId');
33        $isAdmin = (bool)$request->getAttribute('isAdmin', false);
34
35        $loan = $this->loanService->getLoan($loanId);
36        if (!$isAdmin && $loan->investorId !== $investorId) {
37            throw new NotFoundException('Loan not found');
38        }
39
40        $data = ['loan' => $loan->toArray()];
41        if ($loan->status === 'active') {
42            $data['paymentSchedule'] = $this->loanService->getPaymentSchedule($loanId);
43        }
44
45        return $this->renderer->json($response, ['success' => true, 'data' => $data]);
46    }
47}