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