Completed
Push — master ( b0e9fb...0c2496 )
by Tarmo
35s queued 13s
created

GroupsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Controller/v1/Profile/GroupsController.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Controller\v1\Profile;
10
11
use App\Entity\User;
12
use App\Entity\UserGroup;
13
use Nelmio\ApiDocBundle\Annotation\Model;
14
use OpenApi\Annotations as OA;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Routing\Annotation\Route;
19
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
20
use Symfony\Component\Serializer\SerializerInterface;
21
22
/**
23
 * Class GroupsController
24
 *
25
 * @package App\Controller\v1\Profile
26
 * @author TLe, Tarmo Leppänen <[email protected]>
27
 */
28
class GroupsController
29
{
30 17
    public function __construct(
31
        private SerializerInterface $serializer,
32
    ) {
33 17
    }
34
35
    /**
36
     * Endpoint action to get current user user groups.
37
     *
38
     * @OA\Parameter(
39
     *      name="Authorization",
40
     *      in="header",
41
     *      required=true,
42
     *      description="Authorization header",
43
     *      @OA\Schema(
44
     *          type="string",
45
     *          default="Bearer _your_jwt_here_",
46
     *      ),
47
     *  )
48
     * @OA\Response(
49
     *      response=200,
50
     *      description="User groups",
51
     *      @OA\Schema(
52
     *          type="array",
53
     *          @OA\Items(
54
     *              ref=@Model(
55
     *                  type=\App\Entity\UserGroup::class,
56
     *                  groups={"set.UserProfileGroups"},
57
     *              ),
58
     *          ),
59
     *      ),
60
     *  )
61
     * @OA\Response(
62
     *      response=401,
63
     *      description="Invalid token",
64
     *      @OA\Schema(
65
     *          type="object",
66
     *          example={
67
     *              "Token not found": "{code: 401, message: 'JWT Token not found'}",
68
     *              "Expired token": "{code: 401, message: 'Expired JWT Token'}",
69
     *          },
70
     *          @OA\Property(property="code", type="integer", description="Error code"),
71
     *          @OA\Property(property="message", type="string", description="Error description"),
72
     *      ),
73
     *  )
74
     * @OA\Response(
75
     *      response=403,
76
     *      description="Access denied",
77
     *      @OA\Schema(
78
     *          type="403",
79
     *          example={
80
     *              "Access denied": "{code: 403, message: 'Access denied'}",
81
     *          },
82
     *          @OA\Property(property="code", type="integer", description="Error code"),
83
     *          @OA\Property(property="message", type="string", description="Error description"),
84
     *      ),
85
     *  )
86
     * @OA\Tag(name="Profile")
87
     */
88 10
    #[Route(
89
        path: '/v1/profile/groups',
90
        methods: [Request::METHOD_GET],
91
    )]
92
    #[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
93
    public function __invoke(User $loggedInUser): JsonResponse
94
    {
95 10
        return new JsonResponse(
96 10
            $this->serializer->serialize(
97 10
                $loggedInUser->getUserGroups()->toArray(),
98 10
                'json',
99 10
                ['groups' => UserGroup::SET_USER_PROFILE_GROUPS],
100
            ),
101 10
            json: true,
102
        );
103
    }
104
}
105