Total Complexity | 74 |
Total Lines | 603 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AccessUrl 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 AccessUrl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | #[ApiResource( |
||
27 | normalizationContext: [ |
||
28 | 'groups' => ['access_url:read'], |
||
29 | 'swagger_definition_name' => 'Read', |
||
30 | ], |
||
31 | denormalizationContext: [ |
||
32 | 'groups' => ['access_url:write', 'course_category:write'], |
||
33 | ], |
||
34 | security: "is_granted('ROLE_ADMIN')" |
||
35 | )] |
||
36 | #[ORM\Table(name: 'access_url')] |
||
37 | #[Gedmo\Tree(type: 'nested')] |
||
38 | #[ORM\Entity(repositoryClass: AccessUrlRepository::class)] |
||
39 | #[ORM\EntityListeners([AccessUrlListener::class, ResourceListener::class])] |
||
40 | #[ApiResource( |
||
41 | uriTemplate: '/users/{id}/access_urls', |
||
42 | operations: [new GetCollection(controller: UserAccessUrlsController::class)], |
||
43 | uriVariables: [ |
||
44 | 'id' => new Link(description: 'User identifier'), |
||
45 | ], |
||
46 | normalizationContext: ['groups' => ['user_access_url:read']], |
||
47 | paginationEnabled: false, |
||
48 | )] |
||
49 | class AccessUrl extends AbstractResource implements ResourceInterface, Stringable |
||
50 | { |
||
51 | public const DEFAULT_ACCESS_URL = 'http://localhost/'; |
||
52 | #[ORM\Column(name: 'id', type: 'integer')] |
||
53 | #[ORM\Id] |
||
54 | #[ORM\GeneratedValue] |
||
55 | #[Groups(['access_url:read', 'access_url:write'])] |
||
56 | protected ?int $id = null; |
||
57 | |||
58 | /** |
||
59 | * @var Collection<int, AccessUrlRelCourse> |
||
60 | */ |
||
61 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelCourse::class, cascade: ['persist'], orphanRemoval: true)] |
||
62 | protected Collection $courses; |
||
63 | |||
64 | /** |
||
65 | * @var Collection<int, AccessUrlRelSession> |
||
66 | */ |
||
67 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelSession::class, cascade: ['persist'], orphanRemoval: true)] |
||
68 | protected Collection $sessions; |
||
69 | |||
70 | /** |
||
71 | * @var Collection<int, AccessUrlRelUser> |
||
72 | */ |
||
73 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelUser::class, cascade: ['persist'], orphanRemoval: true)] |
||
74 | protected Collection $users; |
||
75 | |||
76 | /** |
||
77 | * @var Collection<int, SettingsCurrent> |
||
78 | */ |
||
79 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: SettingsCurrent::class, cascade: ['persist'], orphanRemoval: true)] |
||
80 | protected Collection $settings; |
||
81 | |||
82 | /** |
||
83 | * @var Collection<int, SessionCategory> |
||
84 | */ |
||
85 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: SessionCategory::class, cascade: ['persist'], orphanRemoval: true)] |
||
86 | protected Collection $sessionCategories; |
||
87 | |||
88 | /** |
||
89 | * @var Collection<int, AccessUrlRelCourseCategory> |
||
90 | */ |
||
91 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelCourseCategory::class, cascade: ['persist'], orphanRemoval: true)] |
||
92 | protected Collection $courseCategory; |
||
93 | |||
94 | #[ORM\JoinColumn(onDelete: 'CASCADE')] |
||
95 | #[Gedmo\TreeParent] |
||
96 | #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
||
97 | protected ?AccessUrl $parent = null; |
||
98 | |||
99 | /** |
||
100 | * @var Collection<int, AccessUrl> |
||
101 | */ |
||
102 | #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)] |
||
103 | #[ORM\OrderBy(['id' => 'ASC'])] |
||
104 | protected Collection $children; |
||
105 | |||
106 | #[Gedmo\TreeLeft] |
||
107 | #[ORM\Column(name: 'lft', type: 'integer')] |
||
108 | protected int $lft; |
||
109 | |||
110 | #[Gedmo\TreeLevel] |
||
111 | #[ORM\Column(name: 'lvl', type: 'integer')] |
||
112 | protected int $lvl; |
||
113 | |||
114 | #[Gedmo\TreeRight] |
||
115 | #[ORM\Column(name: 'rgt', type: 'integer')] |
||
116 | protected int $rgt; |
||
117 | |||
118 | #[Gedmo\TreeRoot] |
||
119 | #[ORM\ManyToOne(targetEntity: self::class)] |
||
120 | #[ORM\JoinColumn(name: 'tree_root', onDelete: 'CASCADE')] |
||
121 | protected ?AccessUrl $root = null; |
||
122 | |||
123 | #[Assert\NotBlank] |
||
124 | #[Groups(['access_url:read', 'access_url:write', 'user_access_url:read'])] |
||
125 | #[ORM\Column(name: 'url', type: 'string', length: 255)] |
||
126 | protected string $url; |
||
127 | |||
128 | #[Groups(['user_access_url:read'])] |
||
129 | #[ORM\Column(name: 'description', type: 'text')] |
||
130 | protected ?string $description = null; |
||
131 | |||
132 | #[ORM\Column(name: 'active', type: 'integer')] |
||
133 | protected int $active; |
||
134 | |||
135 | #[ORM\Column(name: 'created_by', type: 'integer')] |
||
136 | protected int $createdBy; |
||
137 | |||
138 | #[ORM\Column(name: 'tms', type: 'datetime', nullable: true)] |
||
139 | protected ?DateTime $tms; |
||
140 | |||
141 | #[ORM\Column(name: 'url_type', type: 'boolean', nullable: true)] |
||
142 | protected ?bool $urlType = null; |
||
143 | |||
144 | #[ORM\Column(name: 'limit_courses', type: 'integer', nullable: true)] |
||
145 | protected ?int $limitCourses = null; |
||
146 | |||
147 | #[ORM\Column(name: 'limit_active_courses', type: 'integer', nullable: true)] |
||
148 | protected ?int $limitActiveCourses = null; |
||
149 | |||
150 | #[ORM\Column(name: 'limit_sessions', type: 'integer', nullable: true)] |
||
151 | protected ?int $limitSessions = null; |
||
152 | |||
153 | #[ORM\Column(name: 'limit_users', type: 'integer', nullable: true)] |
||
154 | protected ?int $limitUsers = null; |
||
155 | |||
156 | #[ORM\Column(name: 'limit_teachers', type: 'integer', nullable: true)] |
||
157 | protected ?int $limitTeachers = null; |
||
158 | |||
159 | #[ORM\Column(name: 'limit_disk_space', type: 'integer', nullable: true)] |
||
160 | protected ?int $limitDiskSpace = null; |
||
161 | |||
162 | #[Assert\Email] |
||
163 | #[ORM\Column(name: 'email', type: 'string', length: 255, nullable: true)] |
||
164 | protected ?string $email = null; |
||
165 | |||
166 | /** |
||
167 | * @var Collection<int, AccessUrlRelColorTheme> |
||
168 | */ |
||
169 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelColorTheme::class, cascade: ['persist'], orphanRemoval: true)] |
||
170 | private Collection $colorThemes; |
||
171 | |||
172 | /** |
||
173 | * @var Collection<int, AccessUrlRelPlugin> |
||
174 | */ |
||
175 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelPlugin::class, fetch: 'EXTRA_LAZY', orphanRemoval: true)] |
||
176 | private Collection $plugins; |
||
177 | |||
178 | #[ORM\Column(options: ['default' => false])] |
||
179 | private bool $isLoginOnly = false; |
||
180 | |||
181 | public function __construct() |
||
182 | { |
||
183 | $this->description = ''; |
||
184 | $this->tms = new DateTime(); |
||
185 | $this->createdBy = 1; |
||
186 | $this->courses = new ArrayCollection(); |
||
187 | $this->sessions = new ArrayCollection(); |
||
188 | $this->users = new ArrayCollection(); |
||
189 | $this->settings = new ArrayCollection(); |
||
190 | $this->sessionCategories = new ArrayCollection(); |
||
191 | $this->courseCategory = new ArrayCollection(); |
||
192 | $this->children = new ArrayCollection(); |
||
193 | $this->colorThemes = new ArrayCollection(); |
||
194 | $this->plugins = new ArrayCollection(); |
||
195 | } |
||
196 | |||
197 | public function __toString(): string |
||
198 | { |
||
199 | return $this->getUrl(); |
||
200 | } |
||
201 | |||
202 | public function getUrl(): string |
||
203 | { |
||
204 | return $this->url; |
||
205 | } |
||
206 | |||
207 | public function setUrl(string $url): self |
||
208 | { |
||
209 | $this->url = $url; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | public function getDescription(): ?string |
||
217 | } |
||
218 | |||
219 | public function setDescription(string $description): self |
||
220 | { |
||
221 | $this->description = $description; |
||
222 | |||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | public function getActive(): int |
||
227 | { |
||
228 | return $this->active; |
||
229 | } |
||
230 | |||
231 | public function setActive(int $active): self |
||
232 | { |
||
233 | $this->active = $active; |
||
234 | |||
235 | return $this; |
||
236 | } |
||
237 | |||
238 | public function getCreatedBy(): int |
||
239 | { |
||
240 | return $this->createdBy; |
||
241 | } |
||
242 | |||
243 | public function setCreatedBy(int $createdBy): self |
||
244 | { |
||
245 | $this->createdBy = $createdBy; |
||
246 | |||
247 | return $this; |
||
248 | } |
||
249 | |||
250 | public function getTms(): ?DateTime |
||
251 | { |
||
252 | return $this->tms; |
||
253 | } |
||
254 | |||
255 | public function setTms(DateTime $tms): self |
||
256 | { |
||
257 | $this->tms = $tms; |
||
258 | |||
259 | return $this; |
||
260 | } |
||
261 | |||
262 | public function getUrlType(): ?bool |
||
263 | { |
||
264 | return $this->urlType; |
||
265 | } |
||
266 | |||
267 | public function setUrlType(bool $urlType): self |
||
268 | { |
||
269 | $this->urlType = $urlType; |
||
270 | |||
271 | return $this; |
||
272 | } |
||
273 | |||
274 | public function getLimitActiveCourses(): ?int |
||
275 | { |
||
276 | return $this->limitActiveCourses; |
||
277 | } |
||
278 | |||
279 | public function setLimitActiveCourses(int $limitActiveCourses): self |
||
280 | { |
||
281 | $this->limitActiveCourses = $limitActiveCourses; |
||
282 | |||
283 | return $this; |
||
284 | } |
||
285 | |||
286 | public function getLimitSessions(): ?int |
||
287 | { |
||
288 | return $this->limitSessions; |
||
289 | } |
||
290 | |||
291 | public function setLimitSessions(int $limitSessions): self |
||
292 | { |
||
293 | $this->limitSessions = $limitSessions; |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | public function getLimitUsers(): ?int |
||
299 | { |
||
300 | return $this->limitUsers; |
||
301 | } |
||
302 | |||
303 | public function setLimitUsers(int $limitUsers): self |
||
304 | { |
||
305 | $this->limitUsers = $limitUsers; |
||
306 | |||
307 | return $this; |
||
308 | } |
||
309 | |||
310 | public function getLimitTeachers(): ?int |
||
311 | { |
||
312 | return $this->limitTeachers; |
||
313 | } |
||
314 | |||
315 | public function setLimitTeachers(int $limitTeachers): self |
||
320 | } |
||
321 | |||
322 | public function getLimitDiskSpace(): ?int |
||
323 | { |
||
324 | return $this->limitDiskSpace; |
||
325 | } |
||
326 | |||
327 | public function setLimitDiskSpace(int $limitDiskSpace): self |
||
328 | { |
||
329 | $this->limitDiskSpace = $limitDiskSpace; |
||
330 | |||
331 | return $this; |
||
332 | } |
||
333 | |||
334 | public function getEmail(): ?string |
||
335 | { |
||
336 | return $this->email; |
||
337 | } |
||
338 | |||
339 | public function setEmail(string $email): self |
||
340 | { |
||
341 | $this->email = $email; |
||
342 | |||
343 | return $this; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @return Collection<int, SettingsCurrent> |
||
348 | */ |
||
349 | public function getSettings(): Collection |
||
350 | { |
||
351 | return $this->settings; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param Collection<int, SettingsCurrent> $settings |
||
356 | */ |
||
357 | public function setSettings(Collection $settings): self |
||
358 | { |
||
359 | $this->settings = $settings; |
||
360 | |||
361 | return $this; |
||
362 | } |
||
363 | |||
364 | public function getLimitCourses(): ?int |
||
365 | { |
||
366 | return $this->limitCourses; |
||
367 | } |
||
368 | |||
369 | public function setLimitCourses(int $limitCourses): self |
||
370 | { |
||
371 | $this->limitCourses = $limitCourses; |
||
372 | |||
373 | return $this; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @return Collection<int, AccessUrlRelCourse> |
||
378 | */ |
||
379 | public function getCourses(): Collection |
||
380 | { |
||
381 | return $this->courses; |
||
382 | } |
||
383 | |||
384 | public function setCourses(Collection $courses): self |
||
385 | { |
||
386 | $this->courses = $courses; |
||
387 | |||
388 | return $this; |
||
389 | } |
||
390 | |||
391 | public function addCourse(Course $course): self |
||
392 | { |
||
393 | if (!$this->hasCourse($course)) { |
||
394 | $urlRelCourse = (new AccessUrlRelCourse())->setUrl($this); |
||
395 | $course->addUrls($urlRelCourse); |
||
396 | |||
397 | $this->courses->add($urlRelCourse); |
||
398 | } |
||
399 | |||
400 | return $this; |
||
401 | } |
||
402 | |||
403 | public function hasCourse(Course $course): bool |
||
404 | { |
||
405 | if ($this->courses->count() > 0) { |
||
406 | $criteria = Criteria::create()->where(Criteria::expr()->eq('course', $course)); |
||
407 | $found = $this->courses->matching($criteria); |
||
408 | |||
409 | return $found->count() > 0; |
||
410 | } |
||
411 | |||
412 | return false; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @return Collection<int, SessionCategory> |
||
417 | */ |
||
418 | public function getSessionCategories(): Collection |
||
419 | { |
||
420 | return $this->sessionCategories; |
||
421 | } |
||
422 | |||
423 | public function setSessionCategories(Collection $sessionCategories): self |
||
424 | { |
||
425 | $this->sessionCategories = $sessionCategories; |
||
426 | |||
427 | return $this; |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * @return Collection<int, AccessUrlRelSession> |
||
432 | */ |
||
433 | public function getSessions(): Collection |
||
434 | { |
||
435 | return $this->sessions; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @return Collection<int, AccessUrl> |
||
440 | */ |
||
441 | public function getChildren(): Collection |
||
442 | { |
||
443 | return $this->children; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @return Collection<int, AccessUrlRelUser> |
||
448 | */ |
||
449 | public function getUsers(): Collection |
||
450 | { |
||
451 | return $this->users; |
||
452 | } |
||
453 | |||
454 | public function addUser(User $user): self |
||
455 | { |
||
456 | if (!$this->hasUser($user)) { |
||
457 | $accessUrlRelUser = (new AccessUrlRelUser())->setUser($user)->setUrl($this); |
||
458 | $this->users->add($accessUrlRelUser); |
||
459 | } |
||
460 | |||
461 | return $this; |
||
462 | } |
||
463 | |||
464 | public function hasUser(User $user): bool |
||
465 | { |
||
466 | if (0 !== $this->users->count()) { |
||
467 | $criteria = Criteria::create()->where(Criteria::expr()->eq('user', $user)); |
||
468 | $relation = $this->users->matching($criteria); |
||
469 | |||
470 | return $relation->count() > 0; |
||
471 | } |
||
472 | |||
473 | return false; |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * @return Collection<int, AccessUrlRelCourseCategory> |
||
478 | */ |
||
479 | public function getCourseCategory(): Collection |
||
480 | { |
||
481 | return $this->courseCategory; |
||
482 | } |
||
483 | |||
484 | public function getLft(): int |
||
485 | { |
||
486 | return $this->lft; |
||
487 | } |
||
488 | |||
489 | public function getLvl(): int |
||
490 | { |
||
491 | return $this->lvl; |
||
492 | } |
||
493 | |||
494 | public function getRgt(): int |
||
495 | { |
||
496 | return $this->rgt; |
||
497 | } |
||
498 | |||
499 | public function getRoot(): ?self |
||
500 | { |
||
501 | return $this->root; |
||
502 | } |
||
503 | |||
504 | public function getResourceIdentifier(): int |
||
507 | } |
||
508 | |||
509 | public function getId(): ?int |
||
510 | { |
||
511 | return $this->id; |
||
512 | } |
||
513 | |||
514 | public function getResourceName(): string |
||
515 | { |
||
516 | $url = $this->getUrl(); |
||
517 | $url = parse_url($url); |
||
518 | |||
519 | return $url['host']; |
||
520 | } |
||
521 | |||
522 | public function setResourceName(string $name): self |
||
523 | { |
||
524 | return $this->setUrl($name); |
||
525 | } |
||
526 | |||
527 | public function getActiveColorTheme(): ?AccessUrlRelColorTheme |
||
528 | { |
||
529 | $criteria = Criteria::create(); |
||
530 | $criteria->where( |
||
531 | Criteria::expr()->eq('active', true) |
||
532 | ); |
||
533 | |||
534 | return $this->colorThemes->matching($criteria)->first() ?: null; |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * @return Collection<int, AccessUrlRelColorTheme> |
||
539 | */ |
||
540 | public function getColorThemes(): Collection |
||
541 | { |
||
542 | return $this->colorThemes; |
||
543 | } |
||
544 | |||
545 | public function addColorTheme(AccessUrlRelColorTheme $colorTheme): static |
||
546 | { |
||
547 | if (!$this->colorThemes->contains($colorTheme)) { |
||
548 | $this->colorThemes->add($colorTheme); |
||
549 | $colorTheme->setUrl($this); |
||
550 | } |
||
551 | |||
552 | return $this; |
||
553 | } |
||
554 | |||
555 | public function removeColorTheme(AccessUrlRelColorTheme $colorTheme): static |
||
556 | { |
||
557 | if ($this->colorThemes->removeElement($colorTheme)) { |
||
558 | // set the owning side to null (unless already changed) |
||
559 | if ($colorTheme->getUrl() === $this) { |
||
560 | $colorTheme->setUrl(null); |
||
561 | } |
||
562 | } |
||
563 | |||
564 | return $this; |
||
565 | } |
||
566 | |||
567 | public function getColorThemeByTheme(ColorTheme $theme): ?AccessUrlRelColorTheme |
||
568 | { |
||
569 | $criteria = Criteria::create(); |
||
570 | $criteria->where( |
||
571 | Criteria::expr()->eq('colorTheme', $theme) |
||
572 | ); |
||
573 | |||
574 | return $this->colorThemes->matching($criteria)->first() ?: null; |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * @return Collection<int, AccessUrlRelPlugin> |
||
579 | */ |
||
580 | public function getPlugins(): Collection |
||
581 | { |
||
582 | return $this->plugins; |
||
583 | } |
||
584 | |||
585 | public function addPlugin(AccessUrlRelPlugin $plugin): static |
||
586 | { |
||
587 | if (!$this->plugins->contains($plugin)) { |
||
588 | $this->plugins->add($plugin); |
||
589 | $plugin->setUrl($this); |
||
590 | } |
||
591 | |||
592 | return $this; |
||
593 | } |
||
594 | |||
595 | public function removePlugin(AccessUrlRelPlugin $plugin): static |
||
596 | { |
||
597 | if ($this->plugins->removeElement($plugin)) { |
||
598 | // set the owning side to null (unless already changed) |
||
599 | if ($plugin->getUrl() === $this) { |
||
600 | $plugin->setUrl(null); |
||
601 | } |
||
602 | } |
||
603 | |||
604 | return $this; |
||
605 | } |
||
606 | |||
607 | public function isLoginOnly(): bool |
||
608 | { |
||
609 | return $this->isLoginOnly; |
||
610 | } |
||
611 | |||
612 | public function setIsLoginOnly(bool $isLoginOnly): static |
||
613 | { |
||
614 | $this->isLoginOnly = $isLoginOnly; |
||
615 | |||
616 | return $this; |
||
617 | } |
||
618 | |||
619 | public function setSuperior(?AccessUrl $accessUrl): static |
||
624 | } |
||
625 | |||
626 | public function getSuperior(): ?AccessUrl |
||
627 | { |
||
628 | return $this->parent; |
||
629 | } |
||
630 | } |
||
631 |