Passed
Push — dependabot/github_actions/code... ( 154cc6...3e3012 )
by
unknown
26:36 queued 15:59
created

Usergroup   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 383
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 176
c 1
b 0
f 0
dl 0
loc 383
rs 8.8798
wmc 44

40 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A getCourses() 0 3 1
A setDescription() 0 5 1
A setPictureUrl() 0 5 1
A getSessions() 0 3 1
A setQuestions() 0 5 1
A setUsers() 0 5 2
A setTitle() 0 5 1
A removeUsers() 0 5 3
A setUrl() 0 5 1
A setResourceName() 0 3 1
A getUsers() 0 3 1
A setAuthorId() 0 5 1
A __construct() 0 10 1
A getAllowMembersToLeaveGroup() 0 3 1
A setSessions() 0 5 1
A setGroupType() 0 5 1
A getQuestions() 0 3 1
A getDescription() 0 3 1
A getGroupType() 0 3 1
A getVisibility() 0 3 1
A getResourceName() 0 3 1
A setVisibility() 0 5 1
A getPicture() 0 3 1
A getUrl() 0 3 1
A setPicture() 0 5 1
A addUsers() 0 6 1
A setCourses() 0 5 1
A addAccessUrl() 0 8 1
A getPictureUrl() 0 3 1
A setAllowMembersToLeaveGroup() 0 5 1
A __toString() 0 3 1
A getDefaultIllustration() 0 5 2
A getResourceIdentifier() 0 3 1
A getAuthorId() 0 3 1
A addUrlRelUsergroup() 0 6 1
A setMemberCount() 0 5 1
A getMemberCount() 0 3 1
A getId() 0 3 1
A getUrls() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Usergroup often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Usergroup, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Metadata\ApiResource;
10
use ApiPlatform\Metadata\Delete;
11
use ApiPlatform\Metadata\Get;
12
use ApiPlatform\Metadata\GetCollection;
13
use ApiPlatform\Metadata\Post;
14
use ApiPlatform\Metadata\Put;
15
use Chamilo\CoreBundle\DataProvider\GroupMembersDataProvider;
16
use Chamilo\CoreBundle\DataProvider\UsergroupDataProvider;
17
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository;
18
use Chamilo\CoreBundle\State\UsergroupPostProcessor;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\Common\Collections\Collection;
21
use Doctrine\ORM\Mapping as ORM;
22
use Gedmo\Timestampable\Traits\TimestampableEntity;
23
use Stringable;
24
use Symfony\Component\Serializer\Annotation\Groups;
25
use Symfony\Component\Validator\Constraints as Assert;
26
27
/**
28
 * Classes and social groups.
29
 */
30
#[ApiResource(
31
    operations: [
32
        new Get(
33
            uriTemplate: '/usergroup/{id}',
34
            normalizationContext: ['groups' => ['usergroup:read']],
35
            security: "is_granted('ROLE_USER')",
36
            name: 'get_usergroup'
37
        ),
38
        new Put(security: "is_granted('EDIT', object)"),
39
        new Delete(security: "is_granted('DELETE', object)"),
40
        new GetCollection(
41
            uriTemplate: '/usergroup/list/my',
42
            normalizationContext: ['groups' => ['usergroup:read']],
43
            security: "is_granted('ROLE_USER')",
44
            name: 'get_my_usergroups'
45
        ),
46
        new GetCollection(
47
            uriTemplate: '/usergroup/list/newest',
48
            normalizationContext: ['groups' => ['usergroup:read']],
49
            security: "is_granted('ROLE_USER')",
50
            name: 'get_newest_usergroups'
51
        ),
52
        new GetCollection(
53
            uriTemplate: '/usergroup/list/popular',
54
            normalizationContext: ['groups' => ['usergroup:read']],
55
            security: "is_granted('ROLE_USER')",
56
            name: 'get_popular_usergroups'
57
        ),
58
        new GetCollection(
59
            uriTemplate: '/usergroups/search',
60
            normalizationContext: ['groups' => ['usergroup:read']],
61
            security: "is_granted('ROLE_USER')",
62
            name: 'search_usergroups'
63
        ),
64
        new GetCollection(
65
            uriTemplate: '/usergroups/{id}/members',
66
            normalizationContext: ['groups' => ['usergroup:read']],
67
            security: "is_granted('ROLE_USER')",
68
            name: 'get_group_members',
69
            provider: GroupMembersDataProvider::class
70
        ),
71
        new Post(
72
            securityPostDenormalize: "is_granted('CREATE', object)",
73
            processor: UsergroupPostProcessor::class
74
        ),
75
    ],
76
    normalizationContext: [
77
        'groups' => ['usergroup:read'],
78
    ],
79
    denormalizationContext: [
80
        'groups' => ['usergroup:write'],
81
    ],
82
    security: "is_granted('ROLE_USER')",
83
    provider: UsergroupDataProvider::class
84
)]
85
#[ORM\Table(name: 'usergroup')]
86
#[ORM\Entity(repositoryClass: UsergroupRepository::class)]
87
class Usergroup extends AbstractResource implements ResourceInterface, ResourceIllustrationInterface, ResourceWithAccessUrlInterface, Stringable
88
{
89
    use TimestampableEntity;
90
    public const SOCIAL_CLASS = 1;
91
    public const NORMAL_CLASS = 0;
92
    // Definition of constants for user permissions
93
    public const GROUP_USER_PERMISSION_ADMIN = 1; // The admin of a group
94
    public const GROUP_USER_PERMISSION_READER = 2; // A normal user
95
    public const GROUP_USER_PERMISSION_PENDING_INVITATION = 3; // When an admin/moderator invites a user
96
    public const GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER = 4; // A user requests to join a group
97
    public const GROUP_USER_PERMISSION_MODERATOR = 5; // A moderator of the group
98
    public const GROUP_USER_PERMISSION_ANONYMOUS = 6; // An anonymous user, not part of the group
99
    public const GROUP_USER_PERMISSION_HRM = 7; // A human resource manager
100
101
    public const GROUP_PERMISSION_OPEN = 1;
102
    public const GROUP_PERMISSION_CLOSED = 2;
103
104
    #[ORM\Column(name: 'id', type: 'integer', nullable: false)]
105
    #[ORM\Id]
106
    #[ORM\GeneratedValue]
107
    protected ?int $id = null;
108
    #[Assert\NotBlank]
109
    #[Groups(['usergroup:read', 'usergroup:write'])]
110
    #[ORM\Column(name: 'title', type: 'string', length: 255)]
111
    protected string $title;
112
    #[Groups(['usergroup:read', 'usergroup:write'])]
113
    #[ORM\Column(name: 'description', type: 'text', nullable: true)]
114
    protected ?string $description = null;
115
    #[Assert\NotBlank]
116
    #[Groups(['usergroup:read', 'usergroup:write'])]
117
    #[ORM\Column(name: 'group_type', type: 'integer', nullable: false)]
118
    protected int $groupType;
119
    #[ORM\Column(name: 'picture', type: 'string', length: 255, nullable: true)]
120
    protected ?string $picture = null;
121
    #[Groups(['usergroup:read', 'usergroup:write'])]
122
    #[ORM\Column(name: 'url', type: 'string', length: 255, nullable: true)]
123
    protected ?string $url = null;
124
    #[Assert\NotBlank]
125
    #[Groups(['usergroup:read', 'usergroup:write'])]
126
    #[ORM\Column(name: 'visibility', type: 'string', length: 255, nullable: false)]
127
    protected string $visibility;
128
    #[ORM\Column(name: 'author_id', type: 'integer', nullable: true)]
129
    protected ?int $authorId = null;
130
    #[Assert\NotBlank]
131
    #[Groups(['usergroup:read', 'usergroup:write'])]
132
    #[ORM\Column(name: 'allow_members_leave_group', type: 'integer')]
133
    protected int $allowMembersToLeaveGroup;
134
135
    /**
136
     * @var Collection<int, UsergroupRelUser>
137
     */
138
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelUser::class, cascade: ['persist'])]
139
    protected Collection $users;
140
141
    /**
142
     * @var Collection<int, UsergroupRelCourse>
143
     */
144
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelCourse::class, cascade: ['persist'])]
145
    protected Collection $courses;
146
147
    /**
148
     * @var Collection<int, UsergroupRelSession>
149
     */
150
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelSession::class, cascade: ['persist'])]
151
    protected Collection $sessions;
152
153
    /**
154
     * @var Collection<int, UsergroupRelQuestion>
155
     */
156
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelQuestion::class, cascade: ['persist'])]
157
    protected Collection $questions;
158
159
    /**
160
     * @var Collection<int, AccessUrlRelUserGroup>
161
     */
162
    #[ORM\OneToMany(mappedBy: 'userGroup', targetEntity: AccessUrlRelUserGroup::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
163
    protected Collection $urls;
164
165
    #[Groups(['usergroup:read'])]
166
    private ?int $memberCount = null;
167
168
    #[Groups(['usergroup:read'])]
169
    private ?string $pictureUrl = '';
170
171
    public function __construct()
172
    {
173
        $this->groupType = self::NORMAL_CLASS;
174
        $this->visibility = GROUP_PERMISSION_OPEN;
175
        $this->allowMembersToLeaveGroup = 0;
176
        $this->users = new ArrayCollection();
177
        $this->urls = new ArrayCollection();
178
        $this->courses = new ArrayCollection();
179
        $this->sessions = new ArrayCollection();
180
        $this->questions = new ArrayCollection();
181
    }
182
    public function __toString(): string
183
    {
184
        return $this->getTitle();
185
    }
186
187
    /**
188
     * @return Collection<int, UsergroupRelUser>
189
     */
190
    public function getUsers(): Collection
191
    {
192
        return $this->users;
193
    }
194
195
    /**
196
     * @return Collection<int, AccessUrlRelUserGroup>
197
     */
198
    public function getUrls(): Collection
199
    {
200
        return $this->urls;
201
    }
202
    public function addAccessUrl(?AccessUrl $url): self
203
    {
204
        $urlRelUsergroup = new AccessUrlRelUserGroup();
205
        $urlRelUsergroup->setUserGroup($this);
206
        $urlRelUsergroup->setUrl($url);
207
        $this->addUrlRelUsergroup($urlRelUsergroup);
208
209
        return $this;
210
    }
211
    public function addUrlRelUsergroup(AccessUrlRelUserGroup $urlRelUsergroup): self
212
    {
213
        $urlRelUsergroup->setUserGroup($this);
214
        $this->urls[] = $urlRelUsergroup;
215
216
        return $this;
217
    }
218
    public function setUsers(Collection $users): void
219
    {
220
        $this->users = new ArrayCollection();
221
        foreach ($users as $user) {
222
            $this->addUsers($user);
223
        }
224
    }
225
    public function addUsers(UsergroupRelUser $user): self
226
    {
227
        $user->setUsergroup($this);
228
        $this->users[] = $user;
229
230
        return $this;
231
    }
232
    public function removeUsers(UsergroupRelUser $user): void
233
    {
234
        foreach ($this->users as $key => $value) {
235
            if ($value->getId() === $user->getId()) {
236
                unset($this->users[$key]);
237
            }
238
        }
239
    }
240
241
    /**
242
     * Get id.
243
     */
244
    public function getId(): ?int
245
    {
246
        return $this->id;
247
    }
248
    public function setTitle(string $title): self
249
    {
250
        $this->title = $title;
251
252
        return $this;
253
    }
254
    public function getTitle(): string
255
    {
256
        return $this->title;
257
    }
258
    public function setDescription(string $description): self
259
    {
260
        $this->description = $description;
261
262
        return $this;
263
    }
264
    public function getDescription(): ?string
265
    {
266
        return $this->description;
267
    }
268
    public function getGroupType(): int
269
    {
270
        return $this->groupType;
271
    }
272
    public function setGroupType(int $groupType): self
273
    {
274
        $this->groupType = $groupType;
275
276
        return $this;
277
    }
278
    public function getVisibility(): string
279
    {
280
        return $this->visibility;
281
    }
282
    public function setVisibility(string $visibility): self
283
    {
284
        $this->visibility = $visibility;
285
286
        return $this;
287
    }
288
    public function getUrl(): ?string
289
    {
290
        return $this->url;
291
    }
292
    public function setUrl(?string $url): self
293
    {
294
        $this->url = $url;
295
296
        return $this;
297
    }
298
    public function getAuthorId(): ?int
299
    {
300
        return $this->authorId;
301
    }
302
    public function setAuthorId(?int $authorId): self
303
    {
304
        $this->authorId = $authorId;
305
306
        return $this;
307
    }
308
    public function getAllowMembersToLeaveGroup(): int
309
    {
310
        return $this->allowMembersToLeaveGroup;
311
    }
312
    public function setAllowMembersToLeaveGroup(int $allowMembersToLeaveGroup): self
313
    {
314
        $this->allowMembersToLeaveGroup = $allowMembersToLeaveGroup;
315
316
        return $this;
317
    }
318
319
    /**
320
     * @return Collection<int, UsergroupRelCourse>
321
     */
322
    public function getCourses(): Collection
323
    {
324
        return $this->courses;
325
    }
326
    public function setCourses(Collection $courses): self
327
    {
328
        $this->courses = $courses;
329
330
        return $this;
331
    }
332
333
    /**
334
     * @return Collection<int, UsergroupRelSession>
335
     */
336
    public function getSessions(): Collection
337
    {
338
        return $this->sessions;
339
    }
340
    public function setSessions(Collection $sessions): self
341
    {
342
        $this->sessions = $sessions;
343
344
        return $this;
345
    }
346
347
    /**
348
     * @return Collection<int, UsergroupRelQuestion>
349
     */
350
    public function getQuestions(): Collection
351
    {
352
        return $this->questions;
353
    }
354
    public function setQuestions(Collection $questions): self
355
    {
356
        $this->questions = $questions;
357
358
        return $this;
359
    }
360
    public function getPicture(): ?string
361
    {
362
        return $this->picture;
363
    }
364
365
    public function setPicture(?string $picture): self
366
    {
367
        $this->picture = $picture;
368
369
        return $this;
370
    }
371
372
    public function getPictureUrl(): ?string
373
    {
374
        return $this->picture;
375
    }
376
377
    public function setPictureUrl(?string $pictureUrl): self
378
    {
379
        $this->pictureUrl = $pictureUrl;
380
381
        return $this;
382
    }
383
384
    public function getMemberCount(): ?int
385
    {
386
        return $this->memberCount;
387
    }
388
389
    public function setMemberCount(int $memberCount): self
390
    {
391
        $this->memberCount = $memberCount;
392
393
        return $this;
394
    }
395
396
    public function getDefaultIllustration(int $size): string
397
    {
398
        $size = empty($size) ? 32 : $size;
399
400
        return sprintf('/img/icons/%s/group_na.png', $size);
401
    }
402
    public function getResourceIdentifier(): int
403
    {
404
        return $this->getId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getId() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
405
    }
406
    public function getResourceName(): string
407
    {
408
        return $this->getTitle();
409
    }
410
    public function setResourceName(string $name): self
411
    {
412
        return $this->setTitle($name);
413
    }
414
}
415