Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
85.71% covered (warning)
85.71%
6 / 7
80.00% covered (warning)
80.00%
4 / 5
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AuthController
92.86% covered (success)
92.86%
13 / 14
85.71% covered (warning)
85.71%
6 / 7
80.00% covered (warning)
80.00%
4 / 5
66.67% covered (warning)
66.67%
2 / 3
5.20
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
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 me
88.89% covered (warning)
88.89%
8 / 9
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3namespace App\Auth\Infrastructure\Controller;
4
5use App\Auth\Application\Command\GetIdentityQuery;
6use App\Auth\Application\Command\RegistrationCommand;
7use App\Auth\Application\Handler\GetIdentityHandler;
8use App\Auth\Application\Handler\RegistrationHandler;
9use App\Auth\Infrastructure\Dto\IdentityDto;
10use App\Auth\Infrastructure\Dto\RegistrationRequest;
11use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12use Symfony\Component\HttpFoundation\JsonResponse;
13use Symfony\Component\HttpFoundation\Response;
14use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
15use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16use Symfony\Component\ObjectMapper\ObjectMapperInterface;
17use Symfony\Component\Routing\Attribute\Route;
18use Symfony\Component\Security\Core\User\UserInterface;
19use Symfony\Component\Security\Http\Attribute\CurrentUser;
20use Symfony\Component\Security\Http\Attribute\IsGranted;
21
22#[Route('/api/auth')]
23class 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}