Total Complexity | 47 |
Total Lines | 409 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
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 | provider: UsergroupStateProvider::class |
||
46 | ), |
||
47 | new GetCollection( |
||
48 | uriTemplate: '/usergroup/list/newest', |
||
49 | normalizationContext: ['groups' => ['usergroup:read']], |
||
50 | security: "is_granted('ROLE_USER')", |
||
51 | name: 'get_newest_usergroups', |
||
52 | provider: UsergroupStateProvider::class |
||
53 | ), |
||
54 | new GetCollection( |
||
55 | uriTemplate: '/usergroup/list/popular', |
||
56 | normalizationContext: ['groups' => ['usergroup:read']], |
||
57 | security: "is_granted('ROLE_USER')", |
||
58 | name: 'get_popular_usergroups', |
||
59 | provider: UsergroupStateProvider::class |
||
60 | ), |
||
61 | new GetCollection( |
||
62 | uriTemplate: '/usergroups/search', |
||
63 | normalizationContext: ['groups' => ['usergroup:read']], |
||
64 | security: "is_granted('ROLE_USER')", |
||
65 | name: 'search_usergroups', |
||
66 | provider: UsergroupStateProvider::class |
||
67 | ), |
||
68 | new GetCollection( |
||
69 | uriTemplate: '/usergroups/{id}/members', |
||
70 | normalizationContext: ['groups' => ['usergroup:read']], |
||
71 | security: "is_granted('ROLE_USER')", |
||
72 | name: 'get_group_members', |
||
73 | provider: GroupMembersStateProvider::class |
||
74 | ), |
||
75 | new Post( |
||
76 | securityPostDenormalize: "is_granted('CREATE', object)", |
||
77 | processor: UsergroupPostStateProcessor::class |
||
78 | ), |
||
79 | ], |
||
80 | normalizationContext: [ |
||
81 | 'groups' => ['usergroup:read'], |
||
82 | ], |
||
83 | denormalizationContext: [ |
||
84 | 'groups' => ['usergroup:write'], |
||
85 | ], |
||
86 | security: "is_granted('ROLE_USER')", |
||
87 | )] |
||
88 | #[ORM\Table(name: 'usergroup')] |
||
89 | #[ORM\Entity(repositoryClass: UsergroupRepository::class)] |
||
90 | class Usergroup extends AbstractResource implements ResourceInterface, ResourceIllustrationInterface, ResourceWithAccessUrlInterface, Stringable |
||
91 | { |
||
92 | use TimestampableEntity; |
||
93 | public const SOCIAL_CLASS = 1; |
||
94 | public const NORMAL_CLASS = 0; |
||
95 | // Definition of constants for user permissions |
||
96 | public const GROUP_USER_PERMISSION_ADMIN = 1; // The admin of a group |
||
97 | public const GROUP_USER_PERMISSION_READER = 2; // A normal user |
||
98 | public const GROUP_USER_PERMISSION_PENDING_INVITATION = 3; // When an admin/moderator invites a user |
||
99 | public const GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER = 4; // A user requests to join a group |
||
100 | public const GROUP_USER_PERMISSION_MODERATOR = 5; // A moderator of the group |
||
101 | public const GROUP_USER_PERMISSION_ANONYMOUS = 6; // An anonymous user, not part of the group |
||
102 | public const GROUP_USER_PERMISSION_HRM = 7; // A human resource manager |
||
103 | |||
104 | public const GROUP_PERMISSION_OPEN = 1; |
||
105 | public const GROUP_PERMISSION_CLOSED = 2; |
||
106 | |||
107 | #[Groups(['usergroup:read'])] |
||
108 | #[ORM\Column(name: 'id', type: 'integer', nullable: false)] |
||
109 | #[ORM\Id] |
||
110 | #[ORM\GeneratedValue] |
||
111 | protected ?int $id = null; |
||
112 | #[Assert\NotBlank] |
||
113 | #[Groups(['usergroup:read', 'usergroup:write'])] |
||
114 | #[ORM\Column(name: 'title', type: 'string', length: 255)] |
||
115 | protected string $title; |
||
116 | #[Groups(['usergroup:read', 'usergroup:write'])] |
||
117 | #[ORM\Column(name: 'description', type: 'text', nullable: true)] |
||
118 | protected ?string $description = null; |
||
119 | #[Assert\NotBlank] |
||
120 | #[Groups(['usergroup:read', 'usergroup:write'])] |
||
121 | #[ORM\Column(name: 'group_type', type: 'integer', nullable: false)] |
||
122 | protected int $groupType; |
||
123 | #[ORM\Column(name: 'picture', type: 'string', length: 255, nullable: true)] |
||
124 | protected ?string $picture = null; |
||
125 | #[Groups(['usergroup:read', 'usergroup:write'])] |
||
126 | #[ORM\Column(name: 'url', type: 'string', length: 255, nullable: true)] |
||
127 | protected ?string $url = null; |
||
128 | #[Assert\NotBlank] |
||
129 | #[Groups(['usergroup:read', 'usergroup:write'])] |
||
130 | #[ORM\Column(name: 'visibility', type: 'string', length: 255, nullable: false)] |
||
131 | protected string $visibility; |
||
132 | #[ORM\Column(name: 'author_id', type: 'integer', nullable: true)] |
||
133 | protected ?int $authorId = null; |
||
134 | #[Assert\NotBlank] |
||
135 | #[Groups(['usergroup:read', 'usergroup:write'])] |
||
136 | #[ORM\Column(name: 'allow_members_leave_group', type: 'integer')] |
||
137 | protected int $allowMembersToLeaveGroup; |
||
138 | |||
139 | /** |
||
140 | * @var Collection<int, UsergroupRelUser> |
||
141 | */ |
||
142 | #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelUser::class, cascade: ['persist'], orphanRemoval: true)] |
||
143 | protected Collection $users; |
||
144 | |||
145 | /** |
||
146 | * @var Collection<int, UsergroupRelCourse> |
||
147 | */ |
||
148 | #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelCourse::class, cascade: ['persist'])] |
||
149 | protected Collection $courses; |
||
150 | |||
151 | /** |
||
152 | * @var Collection<int, UsergroupRelSession> |
||
153 | */ |
||
154 | #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelSession::class, cascade: ['persist'])] |
||
155 | protected Collection $sessions; |
||
156 | |||
157 | /** |
||
158 | * @var Collection<int, UsergroupRelQuestion> |
||
159 | */ |
||
160 | #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelQuestion::class, cascade: ['persist'])] |
||
161 | protected Collection $questions; |
||
162 | |||
163 | /** |
||
164 | * @var Collection<int, AccessUrlRelUserGroup> |
||
165 | */ |
||
166 | #[ORM\OneToMany(mappedBy: 'userGroup', targetEntity: AccessUrlRelUserGroup::class, cascade: ['persist', 'remove'], orphanRemoval: true)] |
||
167 | protected Collection $urls; |
||
168 | |||
169 | #[Groups(['usergroup:read'])] |
||
170 | private ?int $memberCount = null; |
||
171 | |||
172 | #[Groups(['usergroup:read', 'usergroup:write'])] |
||
173 | private ?string $pictureUrl = ''; |
||
174 | |||
175 | public function __construct() |
||
176 | { |
||
177 | $this->groupType = self::NORMAL_CLASS; |
||
178 | $this->visibility = GROUP_PERMISSION_OPEN; |
||
179 | $this->allowMembersToLeaveGroup = 0; |
||
180 | $this->users = new ArrayCollection(); |
||
181 | $this->urls = new ArrayCollection(); |
||
182 | $this->courses = new ArrayCollection(); |
||
183 | $this->sessions = new ArrayCollection(); |
||
184 | $this->questions = new ArrayCollection(); |
||
185 | } |
||
186 | public function __toString(): string |
||
187 | { |
||
188 | return $this->getTitle(); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return Collection<int, UsergroupRelUser> |
||
193 | */ |
||
194 | public function getUsers(): Collection |
||
195 | { |
||
196 | return $this->users; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return Collection<int, AccessUrlRelUserGroup> |
||
201 | */ |
||
202 | public function getUrls(): Collection |
||
203 | { |
||
204 | return $this->urls; |
||
205 | } |
||
206 | public function addAccessUrl(?AccessUrl $url): self |
||
207 | { |
||
208 | $urlRelUsergroup = new AccessUrlRelUserGroup(); |
||
209 | $urlRelUsergroup->setUserGroup($this); |
||
210 | $urlRelUsergroup->setUrl($url); |
||
211 | $this->addUrlRelUsergroup($urlRelUsergroup); |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | public function addUrlRelUsergroup(AccessUrlRelUserGroup $urlRelUsergroup): self |
||
216 | { |
||
217 | $urlRelUsergroup->setUserGroup($this); |
||
218 | $this->urls[] = $urlRelUsergroup; |
||
219 | |||
220 | return $this; |
||
221 | } |
||
222 | public function setUsers(Collection $users): void |
||
227 | } |
||
228 | } |
||
229 | public function addUsers(UsergroupRelUser $relUser): self |
||
230 | { |
||
231 | $relUser->setUsergroup($this); |
||
232 | $this->users[] = $relUser; |
||
233 | |||
234 | $user = $relUser->getUser(); |
||
235 | |||
236 | foreach ($this->courses as $relcourse) { |
||
237 | $relcourse->getCourse()->addUserAsStudent($user); |
||
238 | } |
||
239 | |||
240 | foreach ($this->sessions as $relSession) { |
||
241 | $relSession->getSession()->addUserInSession(Session::STUDENT, $user); |
||
242 | } |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | public function removeUsers(UsergroupRelUser $user): void |
||
247 | { |
||
248 | foreach ($this->users as $key => $value) { |
||
249 | if ($value->getId() === $user->getId()) { |
||
250 | unset($this->users[$key]); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | public function addUser(User $user, int $relationType = 0): static |
||
256 | { |
||
257 | $rel = (new UsergroupRelUser()) |
||
258 | ->setUser($user) |
||
259 | ->setRelationType($relationType) |
||
260 | ; |
||
261 | |||
262 | $this->addUsers($rel); |
||
263 | |||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Get id. |
||
269 | */ |
||
270 | public function getId(): ?int |
||
271 | { |
||
272 | return $this->id; |
||
273 | } |
||
274 | public function setTitle(string $title): self |
||
275 | { |
||
276 | $this->title = $title; |
||
277 | |||
278 | return $this; |
||
279 | } |
||
280 | public function getTitle(): string |
||
283 | } |
||
284 | public function setDescription(string $description): self |
||
285 | { |
||
286 | $this->description = $description; |
||
287 | |||
288 | return $this; |
||
289 | } |
||
290 | public function getDescription(): ?string |
||
291 | { |
||
292 | return $this->description; |
||
293 | } |
||
294 | public function getGroupType(): int |
||
295 | { |
||
296 | return $this->groupType; |
||
297 | } |
||
298 | public function setGroupType(int $groupType): self |
||
299 | { |
||
300 | $this->groupType = $groupType; |
||
301 | |||
302 | return $this; |
||
303 | } |
||
304 | public function getVisibility(): string |
||
305 | { |
||
306 | return $this->visibility; |
||
307 | } |
||
308 | public function setVisibility(string $visibility): self |
||
309 | { |
||
310 | $this->visibility = $visibility; |
||
311 | |||
312 | return $this; |
||
313 | } |
||
314 | public function getUrl(): ?string |
||
315 | { |
||
316 | return $this->url; |
||
317 | } |
||
318 | public function setUrl(?string $url): self |
||
319 | { |
||
320 | $this->url = $url; |
||
321 | |||
322 | return $this; |
||
323 | } |
||
324 | public function getAuthorId(): ?int |
||
325 | { |
||
326 | return $this->authorId; |
||
327 | } |
||
328 | public function setAuthorId(?int $authorId): self |
||
329 | { |
||
330 | $this->authorId = $authorId; |
||
331 | |||
332 | return $this; |
||
333 | } |
||
334 | public function getAllowMembersToLeaveGroup(): int |
||
335 | { |
||
336 | return $this->allowMembersToLeaveGroup; |
||
337 | } |
||
338 | public function setAllowMembersToLeaveGroup(int $allowMembersToLeaveGroup): self |
||
339 | { |
||
340 | $this->allowMembersToLeaveGroup = $allowMembersToLeaveGroup; |
||
341 | |||
342 | return $this; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return Collection<int, UsergroupRelCourse> |
||
347 | */ |
||
348 | public function getCourses(): Collection |
||
351 | } |
||
352 | public function setCourses(Collection $courses): self |
||
353 | { |
||
354 | $this->courses = $courses; |
||
355 | |||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @return Collection<int, UsergroupRelSession> |
||
361 | */ |
||
362 | public function getSessions(): Collection |
||
363 | { |
||
364 | return $this->sessions; |
||
365 | } |
||
366 | public function setSessions(Collection $sessions): self |
||
367 | { |
||
368 | $this->sessions = $sessions; |
||
369 | |||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @return Collection<int, UsergroupRelQuestion> |
||
375 | */ |
||
376 | public function getQuestions(): Collection |
||
377 | { |
||
378 | return $this->questions; |
||
379 | } |
||
380 | public function setQuestions(Collection $questions): self |
||
381 | { |
||
382 | $this->questions = $questions; |
||
383 | |||
384 | return $this; |
||
385 | } |
||
386 | public function getPicture(): ?string |
||
387 | { |
||
388 | return $this->picture; |
||
389 | } |
||
390 | |||
391 | public function setPicture(?string $picture): self |
||
392 | { |
||
393 | $this->picture = $picture; |
||
394 | |||
395 | return $this; |
||
396 | } |
||
397 | |||
398 | public function getPictureUrl(): ?string |
||
399 | { |
||
400 | return $this->pictureUrl; |
||
401 | } |
||
402 | |||
403 | public function setPictureUrl(?string $pictureUrl): self |
||
408 | } |
||
409 | |||
410 | public function getMemberCount(): ?int |
||
411 | { |
||
412 | return $this->memberCount; |
||
413 | } |
||
414 | |||
415 | public function setMemberCount(int $memberCount): self |
||
416 | { |
||
417 | $this->memberCount = $memberCount; |
||
418 | |||
419 | return $this; |
||
420 | } |
||
421 | |||
422 | public function getDefaultIllustration(int $size): string |
||
423 | { |
||
424 | $size = empty($size) ? 32 : $size; |
||
425 | |||
426 | return \sprintf('/img/icons/%s/group_na.png', $size); |
||
427 | } |
||
428 | public function getResourceIdentifier(): int |
||
429 | { |
||
430 | return $this->getId(); |
||
|
|||
431 | } |
||
432 | public function getResourceName(): string |
||
433 | { |
||
434 | return $this->getTitle(); |
||
435 | } |
||
436 | public function setResourceName(string $name): self |
||
439 | } |
||
440 | } |
||
441 |