Passed
Push — master ( c28736...a1fc90 )
by Tarmo
08:09
created

GroupsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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