Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateUserController
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
5
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
 update
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace App\Identity\Infrastructure\Controllers;
6
7use App\Identity\Application\Command\UpdateUserCommand;
8use App\Identity\Application\Command\UpdateUserHandler;
9use App\Identity\Domain\Exceptions\EmailAlreadyInUse;
10use App\Identity\Domain\Exceptions\UserNotFound;
11use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser;
12use App\Shared\Infrastructure\Http\ReadsJsonPayload;
13use InvalidArgumentException;
14use OpenApi\Attributes as OA;
15use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16use Symfony\Component\HttpFoundation\JsonResponse;
17use Symfony\Component\HttpFoundation\Request;
18use Symfony\Component\Routing\Attribute\Route;
19
20#[OA\Tag(name: 'Users')]
21class UpdateUserController extends AbstractController
22{
23    use ReadsJsonPayload;
24    use ResolvesAuthenticatedUser;
25
26    public function __construct(
27        private readonly UpdateUserHandler $handler,
28    ) {
29    }
30
31    #[Route('/users/me', name: 'users_update', methods: ['PUT'])]
32    public function update(Request $request): JsonResponse
33    {
34        $securityUser = $this->securityUser();
35
36        try {
37            $payload = $this->decodeJsonBody($request);
38
39            $view = $this->handler->handle(new UpdateUserCommand(
40                userId: $securityUser->userId(),
41                email: $this->stringField($payload, 'email'),
42                preferredCurrency: $this->stringField($payload, 'preferredCurrency'),
43                name: $this->stringField($payload, 'name'),
44                surnames: $this->stringField($payload, 'surnames'),
45                birthDate: $this->stringField($payload, 'birthDate'),
46            ));
47        } catch (UserNotFound $e) {
48            return new JsonResponse(['error' => $e->getMessage()], 404);
49        } catch (EmailAlreadyInUse $e) {
50            return new JsonResponse(['error' => $e->getMessage()], 409);
51        } catch (InvalidArgumentException $e) {
52            return new JsonResponse(['error' => $e->getMessage()], 400);
53        }
54
55        return new JsonResponse($view->toArray(), 200);
56    }
57}