Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateUserController
95.65% covered (success)
95.65%
22 / 23
50.00% covered (danger)
50.00%
1 / 2
7
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace App\Identity\Infrastructure\Controllers\Admin;
6
7use App\Identity\Application\Command\AdminUpdateUserCommand;
8use App\Identity\Application\Command\AdminUpdateUserHandler;
9use App\Identity\Domain\Exceptions\AdminSelfAction;
10use App\Identity\Domain\Exceptions\EmailAlreadyInUse;
11use App\Identity\Domain\Exceptions\UserNotFound;
12use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser;
13use App\Identity\Infrastructure\Http\ValidatesUserId;
14use App\Shared\Infrastructure\Http\ReadsJsonPayload;
15use InvalidArgumentException;
16use OpenApi\Attributes as OA;
17use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
18use Symfony\Component\HttpFoundation\JsonResponse;
19use Symfony\Component\HttpFoundation\Request;
20use Symfony\Component\Routing\Attribute\Route;
21
22#[OA\Tag(name: 'Admin')]
23class UpdateUserController extends AbstractController
24{
25    use ResolvesAuthenticatedUser;
26    use ValidatesUserId;
27    use ReadsJsonPayload;
28
29    public function __construct(
30        private readonly AdminUpdateUserHandler $handler,
31    ) {
32    }
33
34    /** Admin-edit a user: profile fields, admin access and account status. Self-lockout guarded. */
35    #[Route('/admin/users/{id}', name: 'admin_users_update', methods: ['PUT'])]
36    public function update(string $id, Request $request): JsonResponse
37    {
38        if ($this->parseUserId($id) === null) {
39            return new JsonResponse(['error' => 'User not found.'], 404);
40        }
41
42        try {
43            $payload = $this->decodeJsonBody($request);
44
45            $view = $this->handler->handle(new AdminUpdateUserCommand(
46                callerId: $this->securityUser()->userId(),
47                targetId: $id,
48                email: $this->stringField($payload, 'email'),
49                preferredCurrency: $this->stringField($payload, 'preferredCurrency'),
50                name: $this->stringField($payload, 'name'),
51                surnames: $this->stringField($payload, 'surnames'),
52                isAdmin: $this->boolField($payload, 'isAdmin'),
53                status: $this->stringField($payload, 'status'),
54            ));
55        } catch (UserNotFound $e) {
56            return new JsonResponse(['error' => $e->getMessage()], 404);
57        } catch (EmailAlreadyInUse $e) {
58            return new JsonResponse(['error' => $e->getMessage()], 409);
59        } catch (AdminSelfAction $e) {
60            return new JsonResponse(['error' => $e->getMessage()], 409);
61        } catch (InvalidArgumentException $e) {
62            return new JsonResponse(['error' => $e->getMessage()], 400);
63        }
64
65        return new JsonResponse($view->toArray(), 200);
66    }
67}