Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UpdateSystemSettingAction | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __invoke | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\SuperAdmin; |
| 6 | |
| 7 | use App\Domain\SystemSettings\Service\SystemSettingsService; |
| 8 | use App\Renderer\JsonRenderer; |
| 9 | use DomainException; |
| 10 | use InvalidArgumentException; |
| 11 | use Psr\Http\Message\ResponseInterface; |
| 12 | use Psr\Http\Message\ServerRequestInterface; |
| 13 | |
| 14 | /** |
| 15 | * Update a system setting. |
| 16 | */ |
| 17 | final readonly class UpdateSystemSettingAction |
| 18 | { |
| 19 | public function __construct( |
| 20 | private SystemSettingsService $settingsService, |
| 21 | private JsonRenderer $renderer |
| 22 | ) {} |
| 23 | |
| 24 | /** |
| 25 | * @param array<string, string> $args |
| 26 | * @param ServerRequestInterface $request |
| 27 | * @param ResponseInterface $response |
| 28 | */ |
| 29 | public function __invoke( |
| 30 | ServerRequestInterface $request, |
| 31 | ResponseInterface $response, |
| 32 | array $args |
| 33 | ): ResponseInterface { |
| 34 | $key = $args['key']; |
| 35 | $data = (array)$request->getParsedBody(); |
| 36 | |
| 37 | // Get current user from JWT |
| 38 | $userId = $request->getAttribute('userId'); |
| 39 | if ($userId === null) { |
| 40 | throw new DomainException('User not authenticated'); |
| 41 | } |
| 42 | |
| 43 | // Validate value |
| 44 | if (!isset($data['value'])) { |
| 45 | throw new InvalidArgumentException('value is required'); |
| 46 | } |
| 47 | |
| 48 | // Handle special case for log level threshold |
| 49 | if ($key === 'log_level_threshold' && isset($data['level'])) { |
| 50 | $setting = $this->settingsService->updateLogLevelThreshold( |
| 51 | $data['level'], |
| 52 | (int)$userId |
| 53 | ); |
| 54 | } else { |
| 55 | // Generic update |
| 56 | $value = is_array($data['value']) ? $data['value'] : ['value' => $data['value']]; |
| 57 | $setting = $this->settingsService->update($key, $value, (int)$userId); |
| 58 | } |
| 59 | |
| 60 | return $this->renderer->json($response, [ |
| 61 | 'success' => true, |
| 62 | 'message' => "Setting '{$key}' updated successfully", |
| 63 | 'data' => $setting, |
| 64 | ]); |
| 65 | } |
| 66 | } |