Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MakeAdditionalPaymentAction
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
12
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 / 16
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Loan;
6
7use App\Domain\Exception\ValidationException;
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 MakeAdditionalPaymentAction
15{
16    public function __construct(
17        private LoanService $loanService,
18        private JsonRenderer $renderer,
19    ) {}
20
21    /**
22     * @param array<string, string> $args
23     * @param ServerRequestInterface $request
24     * @param ResponseInterface $response
25     */
26    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
27    {
28        $loanId = (int)$args['id'];
29        $investorId = Row::int($request->getAttributes(), 'investorId');
30
31        $body = (array)$request->getParsedBody();
32        $amount = Row::nullableString($body, 'amount');
33        if ($amount === null) {
34            throw new ValidationException('amount is required');
35        }
36
37        $result = $this->loanService->makeAdditionalPayment($loanId, $investorId, $amount);
38
39        return $this->renderer->json($response, [
40            'success' => true,
41            'message' => sprintf('Payment of $%s applied to loan #%d', $result['amountPaid'], $loanId),
42            'data' => [
43                'loan' => $result['loan']->toArray(),
44                'paymentId' => $result['paymentId'],
45                'amountPaid' => $result['amountPaid'],
46            ],
47        ]);
48    }
49}