Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApproveLoanAction
0.00% covered (danger)
0.00%
0 / 21
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 / 20
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\Exception\ForbiddenException;
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 ApproveLoanAction
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        $attributes = $request->getAttributes();
29        $role = Row::nullableString($attributes, 'userRole') ?? '';
30        if ($role !== 'admin' && $role !== 'super_admin') {
31            throw new ForbiddenException('Admin access required');
32        }
33
34        $loanId = (int)$args['id'];
35        $adminUserId = Row::int($attributes, 'userId');
36        $body = (array)$request->getParsedBody();
37
38        $loan = $this->loanService->approveLoan(
39            loanId: $loanId,
40            adminUserId: $adminUserId,
41            amount: Row::nullableString($body, 'approvedAmount'),
42            termMonths: Row::nullableInt($body, 'approvedTermMonths'),
43            interestRate: Row::nullableString($body, 'interestRate'),
44            notes: Row::nullableString($body, 'notes'),
45        );
46
47        return $this->renderer->json($response, [
48            'success' => true,
49            'message' => 'Loan approved',
50            'data' => ['loan' => $loan->toArray()],
51        ]);
52    }
53}