Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| GetConfigurationAction | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\Admin; |
| 6 | |
| 7 | use App\Domain\Auth\Data\UserAuthData; |
| 8 | use App\Domain\Configuration\ConfigKeyPolicy; |
| 9 | use App\Domain\Exception\ForbiddenException; |
| 10 | use App\Domain\Loan\Repository\LoanRepository; |
| 11 | use App\Renderer\JsonRenderer; |
| 12 | use Psr\Http\Message\ResponseInterface; |
| 13 | use Psr\Http\Message\ServerRequestInterface; |
| 14 | |
| 15 | /** |
| 16 | * GET /api/admin/configuration |
| 17 | * |
| 18 | * Returns all platform configuration key-value pairs with descriptions, plus |
| 19 | * an editableByCurrentUser flag indicating whether the requester's role may |
| 20 | * update each key. Both admin and super_admin may read; permission to edit |
| 21 | * varies per key (see ConfigKeyPolicy). |
| 22 | */ |
| 23 | final readonly class GetConfigurationAction |
| 24 | { |
| 25 | public function __construct( |
| 26 | private LoanRepository $loanRepository, |
| 27 | private JsonRenderer $renderer, |
| 28 | ) {} |
| 29 | |
| 30 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
| 31 | { |
| 32 | $user = $request->getAttribute('user'); |
| 33 | if (!$user instanceof UserAuthData || !ConfigKeyPolicy::canRoleRead($user->role)) { |
| 34 | throw new ForbiddenException('Admin access required'); |
| 35 | } |
| 36 | |
| 37 | $config = $this->loanRepository->getLoanConfig(); |
| 38 | |
| 39 | $config = array_map( |
| 40 | static fn(array $item): array => [ |
| 41 | ...$item, |
| 42 | 'editableByCurrentUser' => ConfigKeyPolicy::canRoleEdit($user->role, $item['key']), |
| 43 | ], |
| 44 | $config, |
| 45 | ); |
| 46 | |
| 47 | return $this->renderer->json($response, [ |
| 48 | 'success' => true, |
| 49 | 'data' => ['config' => $config], |
| 50 | ]); |
| 51 | } |
| 52 | } |