Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ActivateLoanAction
0.00% covered (danger)
0.00%
0 / 16
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 / 15
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 ActivateLoanAction
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
37        $result = $this->loanService->activateLoan($loanId, $adminUserId);
38
39        return $this->renderer->json($response, [
40            'success' => true,
41            'message' => 'Loan disbursed — funds credited to investor account',
42            'data' => [
43                'loan' => $result['loan']->toArray(),
44                'paymentSchedule' => $result['paymentSchedule'],
45            ],
46        ]);
47    }
48}