Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UpdateConfigurationAction | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| 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\BadRequestException; |
| 10 | use App\Domain\Exception\ForbiddenException; |
| 11 | use App\Domain\Loan\Repository\LoanRepository; |
| 12 | use App\Renderer\JsonRenderer; |
| 13 | use App\Support\Row; |
| 14 | use Psr\Http\Message\ResponseInterface; |
| 15 | use Psr\Http\Message\ServerRequestInterface; |
| 16 | |
| 17 | /** |
| 18 | * PUT /api/admin/configuration/{key} |
| 19 | * |
| 20 | * Updates a single platform configuration value. Admins may edit operational |
| 21 | * keys; super_admin is required for keys that price money or gate eligibility |
| 22 | * (see ConfigKeyPolicy). |
| 23 | */ |
| 24 | final readonly class UpdateConfigurationAction |
| 25 | { |
| 26 | public function __construct( |
| 27 | private LoanRepository $loanRepository, |
| 28 | private JsonRenderer $renderer, |
| 29 | ) {} |
| 30 | |
| 31 | /** |
| 32 | * @param array<string, string> $args |
| 33 | * @param ServerRequestInterface $request |
| 34 | * @param ResponseInterface $response |
| 35 | */ |
| 36 | public function __invoke( |
| 37 | ServerRequestInterface $request, |
| 38 | ResponseInterface $response, |
| 39 | array $args, |
| 40 | ): ResponseInterface { |
| 41 | $user = $request->getAttribute('user'); |
| 42 | if (!$user instanceof UserAuthData || !ConfigKeyPolicy::canRoleRead($user->role)) { |
| 43 | throw new ForbiddenException('Admin access required'); |
| 44 | } |
| 45 | |
| 46 | $key = Row::string($args, 'key'); |
| 47 | |
| 48 | if (!ConfigKeyPolicy::canRoleEdit($user->role, $key)) { |
| 49 | throw new ForbiddenException( |
| 50 | 'This setting can only be modified by a super_admin', |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | $data = (array)$request->getParsedBody(); |
| 55 | |
| 56 | if (!isset($data['value'])) { |
| 57 | throw new BadRequestException('value is required'); |
| 58 | } |
| 59 | |
| 60 | $value = Row::string($data, 'value'); |
| 61 | |
| 62 | $this->loanRepository->updateLoanConfig($key, $value); |
| 63 | |
| 64 | return $this->renderer->json($response, [ |
| 65 | 'success' => true, |
| 66 | 'message' => "Configuration '{$key}' updated successfully", |
| 67 | ]); |
| 68 | } |
| 69 | } |