Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RequestLoanAction
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
20
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 / 17
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Loan;
6
7use App\Domain\Loan\Service\LoanService;
8use App\Renderer\JsonRenderer;
9use InvalidArgumentException;
10use Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13final readonly class RequestLoanAction
14{
15    public function __construct(
16        private LoanService $loanService,
17        private JsonRenderer $renderer
18    ) {}
19
20    public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
21    {
22        $investorId = (int)$request->getAttribute('investorId');
23        $body = (array)$request->getParsedBody();
24
25        $amount = $body['amount'] ?? null;
26        $termMonths = $body['termMonths'] ?? null;
27
28        if ($amount === null) {
29            throw new InvalidArgumentException('Amount is required');
30        }
31        if ($termMonths === null) {
32            throw new InvalidArgumentException('Term (months) is required');
33        }
34
35        $result = $this->loanService->requestLoan($investorId, (string)$amount, (int)$termMonths);
36
37        return $this->renderer->json($response, [
38            'success' => true,
39            'message' => 'Loan approved and activated',
40            'data' => [
41                'loan' => $result['loan']->toArray(),
42                'paymentSchedule' => $result['paymentSchedule'],
43            ],
44        ], 201);
45    }
46}