Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
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 / 15
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 / 14
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 Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13final readonly class ActivateLoanAction
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
35        $result = $this->loanService->activateLoan($loanId, $adminUserId);
36
37        return $this->renderer->json($response, [
38            'success' => true,
39            'message' => 'Loan activated and funds disbursed',
40            'data' => [
41                'loan' => $result['loan']->toArray(),
42                'paymentSchedule' => $result['paymentSchedule'],
43            ],
44        ]);
45    }
46}