Total Complexity | 46 |
Total Lines | 459 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CGroup 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 CGroup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | #[ApiResource( |
||
31 | attributes: [ |
||
32 | 'security' => "is_granted('ROLE_ADMIN')", |
||
33 | ], |
||
34 | normalizationContext: [ |
||
35 | 'groups' => ['group:read'], |
||
36 | ], |
||
37 | )] |
||
38 | class CGroup extends AbstractResource implements ResourceInterface |
||
39 | { |
||
40 | public const TOOL_NOT_AVAILABLE = 0; |
||
41 | public const TOOL_PUBLIC = 1; |
||
42 | public const TOOL_PRIVATE = 2; |
||
43 | public const TOOL_PRIVATE_BETWEEN_USERS = 3; |
||
44 | |||
45 | /** |
||
46 | * @ORM\Column(name="iid", type="integer") |
||
47 | * @ORM\Id |
||
48 | * @ORM\GeneratedValue |
||
49 | * @Groups({"group:read", "group:write"}) |
||
50 | */ |
||
51 | protected int $iid; |
||
52 | |||
53 | /** |
||
54 | * @ORM\Column(name="name", type="string", length=100, nullable=false) |
||
55 | * @Groups({"group:read", "group:write"}) |
||
56 | */ |
||
57 | #[Assert\NotBlank] |
||
58 | protected string $name; |
||
59 | |||
60 | /** |
||
61 | * @ORM\Column(name="status", type="boolean", nullable=false) |
||
62 | */ |
||
63 | #[Assert\NotNull] |
||
64 | protected bool $status; |
||
65 | |||
66 | /** |
||
67 | * @ORM\ManyToOne(targetEntity="CGroupCategory", cascade={"persist"}) |
||
68 | * @ORM\JoinColumn(name="category_id", referencedColumnName="iid", onDelete="CASCADE") |
||
69 | */ |
||
70 | protected ?CGroupCategory $category = null; |
||
71 | |||
72 | /** |
||
73 | * @ORM\Column(name="description", type="text", nullable=true) |
||
74 | */ |
||
75 | protected ?string $description = null; |
||
76 | |||
77 | /** |
||
78 | * @ORM\Column(name="max_student", type="integer") |
||
79 | */ |
||
80 | #[Assert\NotBlank] |
||
81 | protected int $maxStudent; |
||
82 | |||
83 | /** |
||
84 | * @ORM\Column(name="doc_state", type="integer") |
||
85 | */ |
||
86 | protected int $docState; |
||
87 | |||
88 | /** |
||
89 | * @ORM\Column(name="calendar_state", type="integer") |
||
90 | */ |
||
91 | protected int $calendarState; |
||
92 | |||
93 | /** |
||
94 | * @ORM\Column(name="work_state", type="integer") |
||
95 | */ |
||
96 | protected int $workState; |
||
97 | |||
98 | /** |
||
99 | * @ORM\Column(name="announcements_state", type="integer") |
||
100 | */ |
||
101 | protected int $announcementsState; |
||
102 | |||
103 | /** |
||
104 | * @ORM\Column(name="forum_state", type="integer") |
||
105 | */ |
||
106 | protected int $forumState; |
||
107 | |||
108 | /** |
||
109 | * @ORM\Column(name="wiki_state", type="integer") |
||
110 | */ |
||
111 | protected int $wikiState; |
||
112 | |||
113 | /** |
||
114 | * @ORM\Column(name="chat_state", type="integer") |
||
115 | */ |
||
116 | protected int $chatState; |
||
117 | |||
118 | /** |
||
119 | * @ORM\Column(name="self_registration_allowed", type="boolean") |
||
120 | */ |
||
121 | protected bool $selfRegistrationAllowed; |
||
122 | |||
123 | /** |
||
124 | * @ORM\Column(name="self_unregistration_allowed", type="boolean") |
||
125 | */ |
||
126 | protected bool $selfUnregistrationAllowed; |
||
127 | |||
128 | /** |
||
129 | * @ORM\Column(name="document_access", type="integer", options={"default":0}) |
||
130 | */ |
||
131 | protected int $documentAccess; |
||
132 | |||
133 | /** |
||
134 | * @var CGroupRelUser[]|Collection<int, CGroupRelUser> |
||
135 | * |
||
136 | * @ORM\OneToMany(targetEntity="CGroupRelUser", mappedBy="group") |
||
137 | */ |
||
138 | protected Collection $members; |
||
139 | |||
140 | /** |
||
141 | * @var CGroupRelTutor[]|Collection<int, CGroupRelTutor> |
||
142 | * |
||
143 | * @ORM\OneToMany(targetEntity="CGroupRelTutor", mappedBy="group") |
||
144 | */ |
||
145 | protected Collection $tutors; |
||
146 | |||
147 | public function __construct() |
||
148 | { |
||
149 | $this->status = true; |
||
150 | $this->members = new ArrayCollection(); |
||
151 | $this->tutors = new ArrayCollection(); |
||
152 | |||
153 | // Default values |
||
154 | $defaultVisibility = self::TOOL_PRIVATE; |
||
155 | |||
156 | $this->docState = $defaultVisibility; |
||
157 | $this->calendarState = $defaultVisibility; |
||
158 | $this->workState = $defaultVisibility; |
||
159 | $this->announcementsState = $defaultVisibility; |
||
160 | $this->forumState = $defaultVisibility; |
||
161 | $this->wikiState = $defaultVisibility; |
||
162 | $this->chatState = $defaultVisibility; |
||
163 | $this->documentAccess = $defaultVisibility; |
||
164 | |||
165 | $this->selfRegistrationAllowed = false; |
||
166 | $this->selfUnregistrationAllowed = false; |
||
167 | } |
||
168 | |||
169 | public function __toString(): string |
||
170 | { |
||
171 | return $this->getName(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get iid. |
||
176 | * |
||
177 | * @return int |
||
178 | */ |
||
179 | public function getIid() |
||
182 | } |
||
183 | |||
184 | public function setName(string $name): self |
||
185 | { |
||
186 | $this->name = $name; |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | public function getName(): string |
||
192 | { |
||
193 | return $this->name; |
||
194 | } |
||
195 | |||
196 | public function setStatus(bool $status): self |
||
197 | { |
||
198 | $this->status = $status; |
||
199 | |||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | public function getStatus(): bool |
||
204 | { |
||
205 | return $this->status; |
||
206 | } |
||
207 | |||
208 | public function setDescription(string $description): self |
||
209 | { |
||
210 | $this->description = $description; |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | public function getDescription(): ?string |
||
216 | { |
||
217 | return $this->description; |
||
218 | } |
||
219 | |||
220 | public function setMaxStudent(int $maxStudent): self |
||
221 | { |
||
222 | $this->maxStudent = $maxStudent; |
||
223 | |||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | public function getMaxStudent(): int |
||
228 | { |
||
229 | return $this->maxStudent; |
||
230 | } |
||
231 | |||
232 | public function setDocState(int $docState): self |
||
233 | { |
||
234 | $this->docState = $docState; |
||
235 | |||
236 | return $this; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get docState. |
||
241 | * |
||
242 | * @return int |
||
243 | */ |
||
244 | public function getDocState() |
||
245 | { |
||
246 | return $this->docState; |
||
247 | } |
||
248 | |||
249 | public function setCalendarState(int $calendarState): self |
||
250 | { |
||
251 | $this->calendarState = $calendarState; |
||
252 | |||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Get calendarState. |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | public function getCalendarState() |
||
264 | } |
||
265 | |||
266 | public function setWorkState(int $workState): self |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get workState. |
||
275 | * |
||
276 | * @return int |
||
277 | */ |
||
278 | public function getWorkState() |
||
281 | } |
||
282 | |||
283 | public function setAnnouncementsState(int $announcementsState): self |
||
284 | { |
||
285 | $this->announcementsState = $announcementsState; |
||
286 | |||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Get announcementsState. |
||
292 | * |
||
293 | * @return int |
||
294 | */ |
||
295 | public function getAnnouncementsState() |
||
296 | { |
||
297 | return $this->announcementsState; |
||
298 | } |
||
299 | |||
300 | public function setForumState(int $forumState): self |
||
301 | { |
||
302 | $this->forumState = $forumState; |
||
303 | |||
304 | return $this; |
||
305 | } |
||
306 | |||
307 | public function getForumState(): int |
||
308 | { |
||
309 | return $this->forumState; |
||
310 | } |
||
311 | |||
312 | public function setWikiState(int $wikiState): self |
||
313 | { |
||
314 | $this->wikiState = $wikiState; |
||
315 | |||
316 | return $this; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Get wikiState. |
||
321 | * |
||
322 | * @return int |
||
323 | */ |
||
324 | public function getWikiState() |
||
325 | { |
||
326 | return $this->wikiState; |
||
327 | } |
||
328 | |||
329 | public function setChatState(int $chatState): self |
||
330 | { |
||
331 | $this->chatState = $chatState; |
||
332 | |||
333 | return $this; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Get chatState. |
||
338 | * |
||
339 | * @return int |
||
340 | */ |
||
341 | public function getChatState() |
||
342 | { |
||
343 | return $this->chatState; |
||
344 | } |
||
345 | |||
346 | public function setSelfRegistrationAllowed(bool $selfRegistrationAllowed): self |
||
347 | { |
||
348 | $this->selfRegistrationAllowed = $selfRegistrationAllowed; |
||
349 | |||
350 | return $this; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Get selfRegistrationAllowed. |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function getSelfRegistrationAllowed() |
||
359 | { |
||
360 | return $this->selfRegistrationAllowed; |
||
361 | } |
||
362 | |||
363 | public function setSelfUnregistrationAllowed(bool $selfUnregistrationAllowed): self |
||
364 | { |
||
365 | $this->selfUnregistrationAllowed = $selfUnregistrationAllowed; |
||
366 | |||
367 | return $this; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Get selfUnregistrationAllowed. |
||
372 | * |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function getSelfUnregistrationAllowed() |
||
376 | { |
||
377 | return $this->selfUnregistrationAllowed; |
||
378 | } |
||
379 | |||
380 | public function getDocumentAccess(): int |
||
381 | { |
||
382 | return $this->documentAccess; |
||
383 | } |
||
384 | |||
385 | public function setDocumentAccess(int $documentAccess): self |
||
386 | { |
||
387 | $this->documentAccess = $documentAccess; |
||
388 | |||
389 | return $this; |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @return CGroupRelUser[]|Collection |
||
394 | */ |
||
395 | public function getMembers() |
||
396 | { |
||
397 | return $this->members; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @param CGroupRelUser[]|Collection<int, CGroupRelUser> $members |
||
402 | */ |
||
403 | public function setMembers(Collection $members): self |
||
404 | { |
||
405 | $this->members = $members; |
||
406 | |||
407 | return $this; |
||
408 | } |
||
409 | |||
410 | public function hasMembers(): bool |
||
411 | { |
||
412 | return $this->members->count() > 0; |
||
413 | } |
||
414 | |||
415 | public function hasMember(User $user): bool |
||
416 | { |
||
417 | if (!$this->hasMembers()) { |
||
418 | return false; |
||
419 | } |
||
420 | |||
421 | $list = $this->members->filter(function (CGroupRelUser $member) use ($user) { |
||
422 | return $member->getUser()->getId() === $user->getId(); |
||
423 | }); |
||
424 | |||
425 | return $list->count() > 0; |
||
426 | } |
||
427 | |||
428 | public function hasTutor(User $user): bool |
||
429 | { |
||
430 | if (!$this->hasTutors()) { |
||
431 | return false; |
||
432 | } |
||
433 | |||
434 | $list = $this->tutors->filter(function (CGroupRelTutor $tutor) use ($user) { |
||
435 | return $tutor->getUser()->getId() === $user->getId(); |
||
436 | }); |
||
437 | |||
438 | return $list->count() > 0; |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * @return CGroupRelTutor[]|Collection |
||
443 | */ |
||
444 | public function getTutors() |
||
445 | { |
||
446 | return $this->tutors; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @param CGroupRelTutor[]|Collection<int, CGroupRelTutor> $tutors |
||
451 | */ |
||
452 | public function setTutors(Collection $tutors): self |
||
453 | { |
||
454 | $this->tutors = $tutors; |
||
455 | |||
456 | return $this; |
||
457 | } |
||
458 | |||
459 | public function hasTutors(): bool |
||
460 | { |
||
461 | return $this->tutors->count() > 0; |
||
462 | } |
||
463 | |||
464 | public function getCategory(): ?CGroupCategory |
||
465 | { |
||
466 | return $this->category; |
||
467 | } |
||
468 | |||
469 | public function setCategory(CGroupCategory $category = null): self |
||
470 | { |
||
471 | $this->category = $category; |
||
472 | |||
473 | return $this; |
||
474 | } |
||
475 | |||
476 | public function getResourceIdentifier(): int |
||
477 | { |
||
478 | return $this->iid; |
||
479 | } |
||
480 | |||
481 | public function getResourceName(): string |
||
482 | { |
||
483 | return $this->getName(); |
||
484 | } |
||
485 | |||
486 | public function setResourceName(string $name): self |
||
489 | } |
||
490 | } |
||
491 |