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
DenyLoanAction
0.00% covered (danger)
0.00%
0 / 21
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 / 20
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\BadRequestException;
8use App\Domain\Exception\ForbiddenException;
9use App\Domain\Loan\Service\LoanService;
10use App\Renderer\JsonRenderer;
11use App\Support\Row;
12use Psr\Http\Message\ResponseInterface;
13use Psr\Http\Message\ServerRequestInterface;
14
15final readonly class DenyLoanAction
16{
17    public function __construct(
18        private LoanService $loanService,
19        private JsonRenderer $renderer,
20    ) {}
21
22    /**
23     * @param array<string, string> $args
24     * @param ServerRequestInterface $request
25     * @param ResponseInterface $response
26     */
27    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
28    {
29        $attributes = $request->getAttributes();
30        $role = Row::nullableString($attributes, 'userRole') ?? '';
31        if ($role !== 'admin' && $role !== 'super_admin') {
32            throw new ForbiddenException('Admin access required');
33        }
34
35        $loanId = (int)$args['id'];
36        $adminUserId = Row::int($attributes, 'userId');
37        $body = (array)$request->getParsedBody();
38
39        $reason = trim(Row::nullableString($body, 'reason') ?? '');
40        if ($reason === '') {
41            throw new BadRequestException('Denial reason is required');
42        }
43
44        $loan = $this->loanService->denyLoan(
45            loanId: $loanId,
46            adminUserId: $adminUserId,
47            reason: $reason,
48        );
49
50        return $this->renderer->json($response, [
51            'success' => true,
52            'message' => 'Loan denied',
53            'data' => ['loan' => $loan->toArray()],
54        ]);
55    }
56}