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
DenyLoanAction
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
30
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
20
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 InvalidArgumentException;
11use Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13
14final readonly class DenyLoanAction
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        $role = (string)$request->getAttribute('userRole');
29        if ($role !== 'admin' && $role !== 'super_admin') {
30            throw new ForbiddenException('Admin access required');
31        }
32
33        $loanId = (int)$args['id'];
34        $adminUserId = (int)$request->getAttribute('userId');
35        $body = (array)$request->getParsedBody();
36
37        $reason = $body['reason'] ?? '';
38        if (empty(trim((string)$reason))) {
39            throw new InvalidArgumentException('Denial reason is required');
40        }
41
42        $loan = $this->loanService->denyLoan(
43            loanId: $loanId,
44            adminUserId: $adminUserId,
45            reason: (string)$reason,
46        );
47
48        return $this->renderer->json($response, [
49            'success' => true,
50            'message' => 'Loan denied',
51            'data' => ['loan' => $loan->toArray()],
52        ]);
53    }
54}