Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.65% |
22 / 23 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UpdateUserController | |
95.65% |
22 / 23 |
|
50.00% |
1 / 2 |
7 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
95.45% |
21 / 22 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Identity\Infrastructure\Controllers\Admin; |
| 6 | |
| 7 | use App\Identity\Application\Command\AdminUpdateUserCommand; |
| 8 | use App\Identity\Application\Command\AdminUpdateUserHandler; |
| 9 | use App\Identity\Domain\Exceptions\AdminSelfAction; |
| 10 | use App\Identity\Domain\Exceptions\EmailAlreadyInUse; |
| 11 | use App\Identity\Domain\Exceptions\UserNotFound; |
| 12 | use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser; |
| 13 | use App\Identity\Infrastructure\Http\ValidatesUserId; |
| 14 | use App\Shared\Infrastructure\Http\ReadsJsonPayload; |
| 15 | use InvalidArgumentException; |
| 16 | use OpenApi\Attributes as OA; |
| 17 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 18 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 19 | use Symfony\Component\HttpFoundation\Request; |
| 20 | use Symfony\Component\Routing\Attribute\Route; |
| 21 | |
| 22 | #[OA\Tag(name: 'Admin')] |
| 23 | class 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 | } |