Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
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 / 20
0.00% covered (danger)
0.00%
0 / 2
72
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 / 19
0.00% covered (danger)
0.00%
0 / 1
56
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 Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13final readonly class ApproveLoanAction
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        $role = (string)$request->getAttribute('userRole');
28        if ($role !== 'admin' && $role !== 'super_admin') {
29            throw new ForbiddenException('Admin access required');
30        }
31
32        $loanId = (int)$args['id'];
33        $adminUserId = (int)$request->getAttribute('userId');
34        $body = (array)$request->getParsedBody();
35
36        $loan = $this->loanService->approveLoan(
37            loanId: $loanId,
38            adminUserId: $adminUserId,
39            amount: isset($body['approvedAmount']) ? (string)$body['approvedAmount'] : null,
40            termMonths: isset($body['approvedTermMonths']) ? (int)$body['approvedTermMonths'] : null,
41            interestRate: isset($body['interestRate']) ? (string)$body['interestRate'] : null,
42            notes: isset($body['notes']) ? (string)$body['notes'] : null,
43        );
44
45        return $this->renderer->json($response, [
46            'success' => true,
47            'message' => 'Loan approved',
48            'data' => ['loan' => $loan->toArray()],
49        ]);
50    }
51}