Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
92.31% |
12 / 13 |
|
80.00% |
4 / 5 |
|
75.00% |
3 / 4 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UserController | |
92.31% |
12 / 13 |
|
80.00% |
4 / 5 |
|
75.00% |
3 / 4 |
|
66.67% |
2 / 3 |
4.25 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| me | |
88.89% |
8 / 9 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| avatar | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\User\Infrastructure\Controller; |
| 4 | |
| 5 | use App\Shared\Infrastructure\Service\Base64FileDecoder; |
| 6 | use App\User\Application\Command\GetUserByIdentityQuery; |
| 7 | use App\User\Application\Command\UpdateAvatarCommand; |
| 8 | use App\User\Application\Handler\GetUserHandler; |
| 9 | use App\User\Application\Handler\UpdateAvatarHandler; |
| 10 | use App\User\Infrastructure\Dto\UpdateAvatarRequest; |
| 11 | use App\User\Infrastructure\Dto\UserDto; |
| 12 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 13 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 14 | use Symfony\Component\HttpFoundation\Response; |
| 15 | use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; |
| 16 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 17 | use Symfony\Component\ObjectMapper\ObjectMapperInterface; |
| 18 | use Symfony\Component\Routing\Attribute\Route; |
| 19 | use Symfony\Component\Security\Core\User\UserInterface; |
| 20 | use Symfony\Component\Security\Http\Attribute\CurrentUser; |
| 21 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 22 | |
| 23 | #[Route('/api/user')] |
| 24 | class UserController extends AbstractController |
| 25 | { |
| 26 | public function __construct( |
| 27 | private readonly GetUserHandler $getUserHandler, |
| 28 | private readonly UpdateAvatarHandler $updateAvatarHandler, |
| 29 | private readonly ObjectMapperInterface $objectMapper, |
| 30 | private readonly Base64FileDecoder $base64FileDecoder |
| 31 | ) { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | #[Route('/me', name: 'api_user_me', methods: ['GET'])] |
| 36 | #[IsGranted('ROLE_USER')] |
| 37 | public function me(#[CurrentUser] ?UserInterface $user) :JsonResponse |
| 38 | { |
| 39 | $user = ($this->getUserHandler)(new GetUserByIdentityQuery($user->getUserIdentifier())); |
| 40 | |
| 41 | if (!$user) { |
| 42 | throw new NotFoundHttpException('User not found'); |
| 43 | } |
| 44 | return $this->json( |
| 45 | $this->objectMapper->map( |
| 46 | $user, |
| 47 | UserDto::class |
| 48 | ) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | #[Route('/me/avatar', name: 'api_user_me_avatar', methods: ['PUT'])] |
| 53 | #[IsGranted('ROLE_USER')] |
| 54 | public function avatar( |
| 55 | #[MapRequestPayload] UpdateAvatarRequest $updateAvatarRequest, |
| 56 | #[CurrentUser] ?UserInterface $user |
| 57 | ) :JsonResponse { |
| 58 | $tempFile = $this->base64FileDecoder->decodeToTempFile($updateAvatarRequest->getContents(), $updateAvatarRequest->getFileName()); |
| 59 | $u = ($this->updateAvatarHandler)(new UpdateAvatarCommand($user->getUserIdentifier(), $tempFile)); |
| 60 | return $this->json([], Response::HTTP_NO_CONTENT); |
| 61 | } |
| 62 | } |