AttachUserGroupController::__invoke()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 86
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 62
nc 2
nop 2
dl 0
loc 86
ccs 13
cts 13
cp 1
crap 2
rs 8.829
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Controller/v1/User/AttachUserGroupController.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Controller\v1\User;
10
11
use App\Entity\User;
12
use App\Entity\UserGroup;
13
use App\Enum\Role;
14
use App\Resource\UserGroupResource;
15
use App\Resource\UserResource;
16
use Nelmio\ApiDocBundle\Annotation\Model;
17
use OpenApi\Attributes as OA;
18
use OpenApi\Attributes\JsonContent;
19
use OpenApi\Attributes\Property;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Attribute\AsController;
24
use Symfony\Component\Routing\Annotation\Route;
25
use Symfony\Component\Routing\Requirement\Requirement;
26
use Symfony\Component\Security\Http\Attribute\IsGranted;
27
use Symfony\Component\Serializer\SerializerInterface;
28
use Throwable;
29
30
/**
31
 * Class AttachUserGroupController
32
 *
33
 * @package App\Controller\v1\User
34
 * @author TLe, Tarmo Leppänen <[email protected]>
35
 */
36
#[AsController]
37
class AttachUserGroupController
38
{
39 6
    public function __construct(
40
        private readonly UserResource $userResource,
41
        private readonly UserGroupResource $userGroupResource,
42
        private readonly SerializerInterface $serializer,
43
    ) {
44 6
    }
45
46
    /**
47
     * Endpoint action to attach specified user group to specified user.
48
     *
49
     * @throws Throwable
50
     */
51 3
    #[Route(
52
        path: '/v1/user/{user}/group/{userGroup}',
53
        requirements: [
54
            'user' => Requirement::UUID_V1,
55
            'userGroup' => Requirement::UUID_V1,
56
        ],
57
        methods: [Request::METHOD_POST],
58
    )]
59
    #[IsGranted(Role::ROOT->value)]
60
    #[OA\Tag(name: 'User Management')]
61
    #[OA\Parameter(
62
        name: 'Authorization',
63
        description: 'Authorization header',
64
        in: 'header',
65
        required: true,
66
        example: 'Bearer {token}',
67
        allowReserved: true,
68
    )]
69
    #[OA\Parameter(name: 'user', description: 'User GUID', in: 'path', required: true)]
70
    #[OA\Parameter(name: 'userGroup', description: 'User Group GUID', in: 'path', required: true)]
71
    #[OA\Response(
72
        response: 200,
73
        description: 'User groups (user already belongs to this group)',
74
        content: new JsonContent(
75
            type: 'array',
76
            items: new OA\Items(
77
                ref: new Model(type: UserGroup::class, groups: ['UserGroup', 'UserGroup.role']),
78
            ),
79
        ),
80
    )]
81
    #[OA\Response(
82
        response: 201,
83
        description: 'User groups (user added to this group)',
84
        content: new JsonContent(
85
            type: 'array',
86
            items: new OA\Items(
87
                ref: new Model(type: UserGroup::class, groups: ['UserGroup', 'UserGroup.role']),
88
            ),
89
        ),
90
    )]
91
    #[OA\Response(
92
        response: 401,
93
        description: 'Invalid token',
94
        content: new JsonContent(
95
            properties: [
96
                new Property(property: 'code', type: 'integer'),
97
                new Property(property: 'message', type: 'string'),
98
            ],
99
            type: 'object',
100
            example: [
101
                'Token not found' => "{code: 401, message: 'JWT Token not found'}",
102
                'Expired token' => "{code: 401, message: 'Expired JWT Token'}",
103
            ],
104
        ),
105
    )]
106
    #[OA\Response(
107
        response: 403,
108
        description: 'Access denied',
109
        content: new JsonContent(
110
            properties: [
111
                new Property(property: 'code', type: 'integer'),
112
                new Property(property: 'message', type: 'string'),
113
            ],
114
            type: 'object',
115
            example: [
116
                'Access denied' => "{code: 403, message: 'Access denied'}",
117
            ],
118
        ),
119
    )]
120
    public function __invoke(User $user, UserGroup $userGroup): JsonResponse
121
    {
122 3
        $status = $user->getUserGroups()->contains($userGroup) ? Response::HTTP_OK : Response::HTTP_CREATED;
123
124 3
        $this->userResource->save($user->addUserGroup($userGroup), false);
125 3
        $this->userGroupResource->save($userGroup, true, true);
126
127 3
        $groups = [
128 3
            'groups' => [
129 3
                UserGroup::SET_USER_GROUP_BASIC,
130 3
            ],
131 3
        ];
132
133 3
        return new JsonResponse(
134 3
            $this->serializer->serialize($user->getUserGroups()->getValues(), 'json', $groups),
135 3
            $status,
136 3
            json: true
137 3
        );
138
    }
139
}
140