Total Complexity | 62 |
Total Lines | 518 |
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 |
||
21 | #[ApiResource( |
||
22 | normalizationContext: [ |
||
23 | 'groups' => ['access_url:read'], |
||
24 | 'swagger_definition_name' => 'Read', |
||
25 | ], |
||
26 | denormalizationContext: [ |
||
27 | 'groups' => ['access_url:write', 'course_category:write'], |
||
28 | ], |
||
29 | security: "is_granted('ROLE_ADMIN')" |
||
30 | )] |
||
31 | #[ORM\Table(name: 'access_url')] |
||
32 | #[Gedmo\Tree(type: 'nested')] |
||
33 | #[ORM\Entity(repositoryClass: AccessUrlRepository::class)] |
||
34 | class AccessUrl extends AbstractResource implements ResourceInterface, Stringable |
||
35 | { |
||
36 | public const DEFAULT_ACCESS_URL = 'http://localhost/'; |
||
37 | #[ORM\Column(name: 'id', type: 'integer')] |
||
38 | #[ORM\Id] |
||
39 | #[ORM\GeneratedValue] |
||
40 | #[Groups(['access_url:read', 'access_url:write'])] |
||
41 | protected ?int $id = null; |
||
42 | |||
43 | /** |
||
44 | * @var Collection<int, AccessUrlRelCourse> |
||
45 | */ |
||
46 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelCourse::class, cascade: ['persist'], orphanRemoval: true)] |
||
47 | protected Collection $courses; |
||
48 | |||
49 | /** |
||
50 | * @var Collection<int, AccessUrlRelSession> |
||
51 | */ |
||
52 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelSession::class, cascade: ['persist'], orphanRemoval: true)] |
||
53 | protected Collection $sessions; |
||
54 | |||
55 | /** |
||
56 | * @var Collection<int, AccessUrlRelUser> |
||
57 | */ |
||
58 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelUser::class, cascade: ['persist'], orphanRemoval: true)] |
||
59 | protected Collection $users; |
||
60 | |||
61 | /** |
||
62 | * @var Collection<int, SettingsCurrent> |
||
63 | */ |
||
64 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: SettingsCurrent::class, cascade: ['persist'], orphanRemoval: true)] |
||
65 | protected Collection $settings; |
||
66 | |||
67 | /** |
||
68 | * @var Collection<int, SessionCategory> |
||
69 | */ |
||
70 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: SessionCategory::class, cascade: ['persist'], orphanRemoval: true)] |
||
71 | protected Collection $sessionCategories; |
||
72 | |||
73 | /** |
||
74 | * @var Collection<int, AccessUrlRelCourseCategory> |
||
75 | */ |
||
76 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelCourseCategory::class, cascade: ['persist'], orphanRemoval: true)] |
||
77 | protected Collection $courseCategory; |
||
78 | |||
79 | #[ORM\JoinColumn(onDelete: 'CASCADE')] |
||
80 | #[Gedmo\TreeParent] |
||
81 | #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
||
82 | protected ?AccessUrl $parent = null; |
||
83 | |||
84 | /** |
||
85 | * @var Collection<int, AccessUrl> |
||
86 | */ |
||
87 | #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)] |
||
88 | #[ORM\OrderBy(['id' => 'ASC'])] |
||
89 | protected Collection $children; |
||
90 | |||
91 | #[Gedmo\TreeLeft] |
||
92 | #[ORM\Column(name: 'lft', type: 'integer')] |
||
93 | protected int $lft; |
||
94 | |||
95 | #[Gedmo\TreeLevel] |
||
96 | #[ORM\Column(name: 'lvl', type: 'integer')] |
||
97 | protected int $lvl; |
||
98 | |||
99 | #[Gedmo\TreeRight] |
||
100 | #[ORM\Column(name: 'rgt', type: 'integer')] |
||
101 | protected int $rgt; |
||
102 | |||
103 | #[Gedmo\TreeRoot] |
||
104 | #[ORM\ManyToOne(targetEntity: self::class)] |
||
105 | #[ORM\JoinColumn(name: 'tree_root', onDelete: 'CASCADE')] |
||
106 | protected ?AccessUrl $root = null; |
||
107 | |||
108 | #[Assert\NotBlank] |
||
109 | #[Groups(['access_url:read', 'access_url:write'])] |
||
110 | #[ORM\Column(name: 'url', type: 'string', length: 255)] |
||
111 | protected string $url; |
||
112 | |||
113 | #[ORM\Column(name: 'description', type: 'text')] |
||
114 | protected ?string $description = null; |
||
115 | |||
116 | #[ORM\Column(name: 'active', type: 'integer')] |
||
117 | protected int $active; |
||
118 | |||
119 | #[ORM\Column(name: 'created_by', type: 'integer')] |
||
120 | protected int $createdBy; |
||
121 | |||
122 | #[ORM\Column(name: 'tms', type: 'datetime', nullable: true)] |
||
123 | protected ?DateTime $tms; |
||
124 | |||
125 | #[ORM\Column(name: 'url_type', type: 'boolean', nullable: true)] |
||
126 | protected ?bool $urlType = null; |
||
127 | |||
128 | #[ORM\Column(name: 'limit_courses', type: 'integer', nullable: true)] |
||
129 | protected ?int $limitCourses = null; |
||
130 | |||
131 | #[ORM\Column(name: 'limit_active_courses', type: 'integer', nullable: true)] |
||
132 | protected ?int $limitActiveCourses = null; |
||
133 | |||
134 | #[ORM\Column(name: 'limit_sessions', type: 'integer', nullable: true)] |
||
135 | protected ?int $limitSessions = null; |
||
136 | |||
137 | #[ORM\Column(name: 'limit_users', type: 'integer', nullable: true)] |
||
138 | protected ?int $limitUsers = null; |
||
139 | |||
140 | #[ORM\Column(name: 'limit_teachers', type: 'integer', nullable: true)] |
||
141 | protected ?int $limitTeachers = null; |
||
142 | |||
143 | #[ORM\Column(name: 'limit_disk_space', type: 'integer', nullable: true)] |
||
144 | protected ?int $limitDiskSpace = null; |
||
145 | |||
146 | #[Assert\Email] |
||
147 | #[ORM\Column(name: 'email', type: 'string', length: 255, nullable: true)] |
||
148 | protected ?string $email = null; |
||
149 | |||
150 | /** |
||
151 | * @var Collection<int, AccessUrlRelColorTheme> |
||
152 | */ |
||
153 | #[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelColorTheme::class, orphanRemoval: true)] |
||
154 | private Collection $colorThemes; |
||
155 | |||
156 | public function __construct() |
||
157 | { |
||
158 | $this->description = ''; |
||
159 | $this->tms = new DateTime(); |
||
160 | $this->createdBy = 1; |
||
161 | $this->courses = new ArrayCollection(); |
||
162 | $this->sessions = new ArrayCollection(); |
||
163 | $this->users = new ArrayCollection(); |
||
164 | $this->settings = new ArrayCollection(); |
||
165 | $this->sessionCategories = new ArrayCollection(); |
||
166 | $this->courseCategory = new ArrayCollection(); |
||
167 | $this->children = new ArrayCollection(); |
||
168 | $this->colorThemes = new ArrayCollection(); |
||
169 | } |
||
170 | |||
171 | public function __toString(): string |
||
172 | { |
||
173 | return $this->getUrl(); |
||
174 | } |
||
175 | |||
176 | public function getUrl(): string |
||
177 | { |
||
178 | return $this->url; |
||
179 | } |
||
180 | |||
181 | public function setUrl(string $url): self |
||
182 | { |
||
183 | $this->url = $url; |
||
184 | |||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | public function getDescription(): ?string |
||
189 | { |
||
190 | return $this->description; |
||
191 | } |
||
192 | |||
193 | public function setDescription(string $description): self |
||
198 | } |
||
199 | |||
200 | public function getActive(): int |
||
201 | { |
||
202 | return $this->active; |
||
203 | } |
||
204 | |||
205 | public function setActive(int $active): self |
||
206 | { |
||
207 | $this->active = $active; |
||
208 | |||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | public function getCreatedBy(): int |
||
213 | { |
||
214 | return $this->createdBy; |
||
215 | } |
||
216 | |||
217 | public function setCreatedBy(int $createdBy): self |
||
218 | { |
||
219 | $this->createdBy = $createdBy; |
||
220 | |||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | public function getTms(): ?DateTime |
||
225 | { |
||
226 | return $this->tms; |
||
227 | } |
||
228 | |||
229 | public function setTms(DateTime $tms): self |
||
234 | } |
||
235 | |||
236 | public function getUrlType(): ?bool |
||
237 | { |
||
238 | return $this->urlType; |
||
239 | } |
||
240 | |||
241 | public function setUrlType(bool $urlType): self |
||
242 | { |
||
243 | $this->urlType = $urlType; |
||
244 | |||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | public function getLimitActiveCourses(): ?int |
||
249 | { |
||
250 | return $this->limitActiveCourses; |
||
251 | } |
||
252 | |||
253 | public function setLimitActiveCourses(int $limitActiveCourses): self |
||
254 | { |
||
255 | $this->limitActiveCourses = $limitActiveCourses; |
||
256 | |||
257 | return $this; |
||
258 | } |
||
259 | |||
260 | public function getLimitSessions(): ?int |
||
261 | { |
||
262 | return $this->limitSessions; |
||
263 | } |
||
264 | |||
265 | public function setLimitSessions(int $limitSessions): self |
||
266 | { |
||
267 | $this->limitSessions = $limitSessions; |
||
268 | |||
269 | return $this; |
||
270 | } |
||
271 | |||
272 | public function getLimitUsers(): ?int |
||
273 | { |
||
274 | return $this->limitUsers; |
||
275 | } |
||
276 | |||
277 | public function setLimitUsers(int $limitUsers): self |
||
278 | { |
||
279 | $this->limitUsers = $limitUsers; |
||
280 | |||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | public function getLimitTeachers(): ?int |
||
285 | { |
||
286 | return $this->limitTeachers; |
||
287 | } |
||
288 | |||
289 | public function setLimitTeachers(int $limitTeachers): self |
||
290 | { |
||
291 | $this->limitTeachers = $limitTeachers; |
||
292 | |||
293 | return $this; |
||
294 | } |
||
295 | |||
296 | public function getLimitDiskSpace(): ?int |
||
299 | } |
||
300 | |||
301 | public function setLimitDiskSpace(int $limitDiskSpace): self |
||
302 | { |
||
303 | $this->limitDiskSpace = $limitDiskSpace; |
||
304 | |||
305 | return $this; |
||
306 | } |
||
307 | |||
308 | public function getEmail(): ?string |
||
309 | { |
||
310 | return $this->email; |
||
311 | } |
||
312 | |||
313 | public function setEmail(string $email): self |
||
314 | { |
||
315 | $this->email = $email; |
||
316 | |||
317 | return $this; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @return Collection<int, SettingsCurrent> |
||
322 | */ |
||
323 | public function getSettings(): Collection |
||
324 | { |
||
325 | return $this->settings; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param Collection<int, SettingsCurrent> $settings |
||
330 | */ |
||
331 | public function setSettings(Collection $settings): self |
||
332 | { |
||
333 | $this->settings = $settings; |
||
334 | |||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | public function getLimitCourses(): ?int |
||
339 | { |
||
340 | return $this->limitCourses; |
||
341 | } |
||
342 | |||
343 | public function setLimitCourses(int $limitCourses): self |
||
344 | { |
||
345 | $this->limitCourses = $limitCourses; |
||
346 | |||
347 | return $this; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @return Collection<int, AccessUrlRelCourse> |
||
352 | */ |
||
353 | public function getCourses(): Collection |
||
354 | { |
||
355 | return $this->courses; |
||
356 | } |
||
357 | |||
358 | public function setCourses(Collection $courses): self |
||
359 | { |
||
360 | $this->courses = $courses; |
||
361 | |||
362 | return $this; |
||
363 | } |
||
364 | |||
365 | public function addCourse(Course $course): self |
||
366 | { |
||
367 | if (!$this->hasCourse($course)) { |
||
368 | $urlRelCourse = (new AccessUrlRelCourse())->setUrl($this); |
||
369 | $course->addUrls($urlRelCourse); |
||
370 | |||
371 | $this->courses->add($urlRelCourse); |
||
372 | } |
||
373 | |||
374 | return $this; |
||
375 | } |
||
376 | |||
377 | public function hasCourse(Course $course): bool |
||
378 | { |
||
379 | if ($this->courses->count() > 0) { |
||
380 | $criteria = Criteria::create()->where(Criteria::expr()->eq('course', $course)); |
||
381 | $found = $this->courses->matching($criteria); |
||
382 | |||
383 | return $found->count() > 0; |
||
384 | } |
||
385 | |||
386 | return false; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @return Collection<int, SessionCategory> |
||
391 | */ |
||
392 | public function getSessionCategories(): Collection |
||
393 | { |
||
394 | return $this->sessionCategories; |
||
395 | } |
||
396 | |||
397 | public function setSessionCategories(Collection $sessionCategories): self |
||
398 | { |
||
399 | $this->sessionCategories = $sessionCategories; |
||
400 | |||
401 | return $this; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @return Collection<int, AccessUrlRelSession> |
||
406 | */ |
||
407 | public function getSessions(): Collection |
||
408 | { |
||
409 | return $this->sessions; |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * @return Collection<int, AccessUrl> |
||
414 | */ |
||
415 | public function getChildren(): Collection |
||
416 | { |
||
417 | return $this->children; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @return Collection<int, AccessUrlRelUser> |
||
422 | */ |
||
423 | public function getUsers(): Collection |
||
424 | { |
||
425 | return $this->users; |
||
426 | } |
||
427 | |||
428 | public function addUser(User $user): self |
||
429 | { |
||
430 | if (!$this->hasUser($user)) { |
||
431 | $accessUrlRelUser = (new AccessUrlRelUser())->setUser($user)->setUrl($this); |
||
432 | $this->users->add($accessUrlRelUser); |
||
433 | } |
||
434 | |||
435 | return $this; |
||
436 | } |
||
437 | |||
438 | public function hasUser(User $user): bool |
||
439 | { |
||
440 | if (0 !== $this->users->count()) { |
||
441 | $criteria = Criteria::create()->where(Criteria::expr()->eq('user', $user)); |
||
442 | $relation = $this->users->matching($criteria); |
||
443 | |||
444 | return $relation->count() > 0; |
||
445 | } |
||
446 | |||
447 | return false; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * @return Collection<int, AccessUrlRelCourseCategory> |
||
452 | */ |
||
453 | public function getCourseCategory(): Collection |
||
454 | { |
||
455 | return $this->courseCategory; |
||
456 | } |
||
457 | |||
458 | public function getLft(): int |
||
459 | { |
||
460 | return $this->lft; |
||
461 | } |
||
462 | |||
463 | public function getLvl(): int |
||
464 | { |
||
465 | return $this->lvl; |
||
466 | } |
||
467 | |||
468 | public function getRgt(): int |
||
469 | { |
||
470 | return $this->rgt; |
||
471 | } |
||
472 | |||
473 | public function getRoot(): ?self |
||
474 | { |
||
475 | return $this->root; |
||
476 | } |
||
477 | |||
478 | public function getResourceIdentifier(): int |
||
481 | } |
||
482 | |||
483 | public function getId(): ?int |
||
484 | { |
||
485 | return $this->id; |
||
486 | } |
||
487 | |||
488 | public function getResourceName(): string |
||
494 | } |
||
495 | |||
496 | public function setResourceName(string $name): self |
||
497 | { |
||
498 | return $this->setUrl($name); |
||
499 | } |
||
500 | |||
501 | public function getActiveColorTheme(): ?AccessUrlRelColorTheme |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * @return Collection<int, AccessUrlRelColorTheme> |
||
513 | */ |
||
514 | public function getColorThemes(): Collection |
||
515 | { |
||
516 | return $this->colorThemes; |
||
517 | } |
||
518 | |||
519 | public function addColorTheme(AccessUrlRelColorTheme $colorTheme): static |
||
527 | } |
||
528 | |||
529 | public function removeColorTheme(AccessUrlRelColorTheme $colorTheme): static |
||
530 | { |
||
539 | } |
||
540 | } |
||
541 |