Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
92.86% |
13 / 14 |
|
85.71% |
6 / 7 |
|
80.00% |
4 / 5 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AuthController | |
92.86% |
13 / 14 |
|
85.71% |
6 / 7 |
|
80.00% |
4 / 5 |
|
66.67% |
2 / 3 |
5.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register | |
100.00% |
4 / 4 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| me | |
88.89% |
8 / 9 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Auth\Infrastructure\Controller; |
| 4 | |
| 5 | use App\Auth\Application\Command\GetIdentityQuery; |
| 6 | use App\Auth\Application\Command\RegistrationCommand; |
| 7 | use App\Auth\Application\Handler\GetIdentityHandler; |
| 8 | use App\Auth\Application\Handler\RegistrationHandler; |
| 9 | use App\Auth\Infrastructure\Dto\IdentityDto; |
| 10 | use App\Auth\Infrastructure\Dto\RegistrationRequest; |
| 11 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 12 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 13 | use Symfony\Component\HttpFoundation\Response; |
| 14 | use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; |
| 15 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 16 | use Symfony\Component\ObjectMapper\ObjectMapperInterface; |
| 17 | use Symfony\Component\Routing\Attribute\Route; |
| 18 | use Symfony\Component\Security\Core\User\UserInterface; |
| 19 | use Symfony\Component\Security\Http\Attribute\CurrentUser; |
| 20 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 21 | |
| 22 | #[Route('/api/auth')] |
| 23 | class AuthController extends AbstractController |
| 24 | { |
| 25 | public function __construct( |
| 26 | private readonly RegistrationHandler $registrationHandler, |
| 27 | private readonly GetIdentityHandler $getIdentityHandler, |
| 28 | private readonly ObjectMapperInterface $objectMapper, |
| 29 | ) { |
| 30 | } |
| 31 | |
| 32 | #[Route('/registration', name: 'api_auth_registration', methods: ['POST'])] |
| 33 | public function register( |
| 34 | #[MapRequestPayload] RegistrationRequest $registerUserRequest, |
| 35 | #[CurrentUser] ?UserInterface $user |
| 36 | ) :JsonResponse { |
| 37 | if ($user) { |
| 38 | return $this->json(''); |
| 39 | } |
| 40 | ($this->registrationHandler)($this->objectMapper->map($registerUserRequest, RegistrationCommand::class)); |
| 41 | return $this->json(null, Response::HTTP_NO_CONTENT); |
| 42 | } |
| 43 | |
| 44 | #[Route('/me', name: 'api_auth_me', methods: ['GET'])] |
| 45 | #[IsGranted('ROLE_USER')] |
| 46 | public function me(#[CurrentUser] ?UserInterface $user) :JsonResponse |
| 47 | { |
| 48 | $identity = ($this->getIdentityHandler)(new GetIdentityQuery($user->getUserIdentifier())); |
| 49 | |
| 50 | if (!$identity) { |
| 51 | throw new NotFoundHttpException('User not found'); |
| 52 | } |
| 53 | return $this->json( |
| 54 | $this->objectMapper->map( |
| 55 | $identity, |
| 56 | IdentityDto::class |
| 57 | ) |
| 58 | ); |
| 59 | } |
| 60 | } |