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

IndexController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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