Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateUserRoleAction
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\SuperAdmin;
6
7use App\Domain\Auth\Data\UserAuthData;
8use App\Domain\Exception\ForbiddenException;
9use App\Domain\SuperAdmin\Service\SuperAdminService;
10use App\Renderer\JsonRenderer;
11use App\Support\Row;
12use Psr\Http\Message\ResponseInterface;
13use Psr\Http\Message\ServerRequestInterface;
14
15/**
16 * Update a user's role (promote/demote to admin or super_admin).
17 */
18final readonly class UpdateUserRoleAction
19{
20    public function __construct(
21        private JsonRenderer $renderer,
22        private SuperAdminService $service,
23    ) {}
24
25    /**
26     * @param array<string, string> $args
27     * @param ServerRequestInterface $request
28     * @param ResponseInterface $response
29     */
30    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
31    {
32        $user = $request->getAttribute('user');
33        if (!$user instanceof UserAuthData || $user->role !== 'super_admin') {
34            throw new ForbiddenException('Super admin access required');
35        }
36
37        $userId = (int)$args['userId'];
38        $data = (array)$request->getParsedBody();
39        $role = Row::nullableString($data, 'role') ?? '';
40
41        $result = $this->service->updateUserRole($user->userId, $userId, $role);
42
43        return $this->renderer->json($response, [
44            'success' => true,
45            'message' => "User role updated to {$result['role']}",
46            'data' => $result,
47        ]);
48    }
49}