Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MakePaymentAction
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 2
6
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 / 12
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Loan;
6
7use App\Domain\Loan\Service\LoanService;
8use App\Renderer\JsonRenderer;
9use App\Support\Row;
10use Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13final readonly class MakePaymentAction
14{
15    public function __construct(
16        private LoanService $loanService,
17        private JsonRenderer $renderer,
18    ) {}
19
20    /**
21     * @param array<string, string> $args
22     * @param ServerRequestInterface $request
23     * @param ResponseInterface $response
24     */
25    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
26    {
27        $loanId = (int)$args['id'];
28        $investorId = Row::int($request->getAttributes(), 'investorId');
29
30        $result = $this->loanService->makePayment($loanId, $investorId);
31
32        return $this->renderer->json($response, [
33            'success' => true,
34            'message' => sprintf('Payment of $%s applied to loan #%d', $result['amountPaid'], $loanId),
35            'data' => [
36                'loan' => $result['loan']->toArray(),
37                'paymentId' => $result['paymentId'],
38                'amountPaid' => $result['amountPaid'],
39            ],
40        ]);
41    }
42}