Total Complexity | 62 |
Total Lines | 471 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Skill 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 Skill, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | #[ApiResource( |
||
32 | operations: [ |
||
33 | new Post(), |
||
34 | new Patch(), |
||
35 | new Put(), |
||
36 | new Delete(), |
||
37 | new GetCollection( |
||
38 | uriTemplate: '/skills/tree.{_format}', |
||
39 | paginationEnabled: false, |
||
40 | normalizationContext: [ |
||
41 | 'groups' => ['skill:tree:read'] |
||
42 | ], |
||
43 | output: SkillTreeNode::class, |
||
44 | provider: SkillTreeStateProvider::class |
||
45 | ), |
||
46 | new GetCollection(), |
||
47 | new Get(), |
||
48 | ], |
||
49 | normalizationContext: [ |
||
50 | 'groups' => ['skill:read'], |
||
51 | ], |
||
52 | security: "is_granted('ROLE_ADMIN')" |
||
53 | )] |
||
54 | #[ApiFilter(SearchFilter::class, properties: ['issuedSkills.user' => 'exact'])] |
||
55 | #[ApiFilter(SearchFilter::class, properties: ['title' => 'partial'])] |
||
56 | #[ORM\Table(name: 'skill')] |
||
57 | #[ORM\Entity(repositoryClass: SkillRepository::class)] |
||
58 | class Skill implements Stringable, Translatable |
||
59 | { |
||
60 | public const STATUS_DISABLED = 0; |
||
61 | public const STATUS_ENABLED = 1; |
||
62 | |||
63 | #[Groups(['skill:read', 'skill_profile:read'])] |
||
64 | #[ORM\Column(name: 'id', type: 'integer')] |
||
65 | #[ORM\Id] |
||
66 | #[ORM\GeneratedValue] |
||
67 | protected ?int $id = null; |
||
68 | |||
69 | #[ORM\ManyToOne(targetEntity: SkillLevelProfile::class, inversedBy: 'skills')] |
||
70 | #[ORM\JoinColumn(name: 'profile_id', referencedColumnName: 'id')] |
||
71 | protected ?SkillLevelProfile $levelProfile = null; |
||
72 | |||
73 | /** |
||
74 | * @var Collection<int, SkillRelUser> |
||
75 | */ |
||
76 | #[Groups(['skill:read'])] |
||
77 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelUser::class, cascade: ['persist'])] |
||
78 | protected Collection $issuedSkills; |
||
79 | |||
80 | /** |
||
81 | * @var Collection<int, SkillRelItem> |
||
82 | */ |
||
83 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelItem::class, cascade: ['persist'])] |
||
84 | protected Collection $items; |
||
85 | |||
86 | /** |
||
87 | * @var Collection<int, SkillRelSkill> |
||
88 | */ |
||
89 | #[ORM\OneToMany(mappedBy: 'parent', targetEntity: SkillRelSkill::class, cascade: ['persist'])] |
||
90 | protected Collection $skills; |
||
91 | |||
92 | /** |
||
93 | * @var Collection<int, SkillRelCourse> |
||
94 | */ |
||
95 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelCourse::class, cascade: ['persist'])] |
||
96 | protected Collection $courses; |
||
97 | |||
98 | /** |
||
99 | * @var Collection<int, SkillRelGradebook> |
||
100 | */ |
||
101 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelGradebook::class, cascade: ['persist'])] |
||
102 | protected Collection $gradeBookCategories; |
||
103 | |||
104 | #[Gedmo\Translatable] |
||
105 | #[Assert\NotBlank] |
||
106 | #[Groups(['skill:read', 'skill:write', 'skill_rel_user:read'])] |
||
107 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
108 | protected string $title; |
||
109 | |||
110 | #[Gedmo\Translatable] |
||
111 | #[Assert\NotBlank] |
||
112 | #[Groups(['skill:read', 'skill:write'])] |
||
113 | #[ORM\Column(name: 'short_code', type: 'string', length: 100, nullable: false)] |
||
114 | protected string $shortCode; |
||
115 | |||
116 | #[Groups(['skill:read', 'skill:write'])] |
||
117 | #[ORM\Column(name: 'description', type: 'text', nullable: false)] |
||
118 | protected string $description; |
||
119 | |||
120 | #[Assert\NotNull] |
||
121 | #[ORM\Column(name: 'access_url_id', type: 'integer', nullable: false)] |
||
122 | protected int $accessUrlId; |
||
123 | |||
124 | #[Groups(['skill:read', 'skill_rel_user:read'])] |
||
125 | #[ORM\Column(name: 'icon', type: 'string', length: 255, nullable: false)] |
||
126 | protected string $icon; |
||
127 | |||
128 | #[ORM\ManyToOne(targetEntity: Asset::class, cascade: ['persist', 'remove'])] |
||
129 | #[ORM\JoinColumn(name: 'asset_id', referencedColumnName: 'id')] |
||
130 | protected ?Asset $asset = null; |
||
131 | |||
132 | #[ORM\Column(name: 'criteria', type: 'text', nullable: true)] |
||
133 | protected ?string $criteria = null; |
||
134 | |||
135 | #[ORM\Column(name: 'status', type: 'integer', nullable: false, options: ['default' => 1])] |
||
136 | protected int $status; |
||
137 | |||
138 | #[Gedmo\Timestampable(on: 'update')] |
||
139 | #[ORM\Column(name: 'updated_at', type: 'datetime', nullable: false)] |
||
140 | protected DateTime $updatedAt; |
||
141 | |||
142 | #[Gedmo\Locale] |
||
143 | private ?string $locale = null; |
||
144 | |||
145 | /** |
||
146 | * @var Collection<int, SkillRelProfile> |
||
147 | */ |
||
148 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelProfile::class, cascade: ['persist'])] |
||
149 | private Collection $profiles; |
||
150 | |||
151 | public function __construct() |
||
152 | { |
||
153 | $this->issuedSkills = new ArrayCollection(); |
||
154 | $this->items = new ArrayCollection(); |
||
155 | $this->courses = new ArrayCollection(); |
||
156 | $this->gradeBookCategories = new ArrayCollection(); |
||
157 | $this->skills = new ArrayCollection(); |
||
158 | $this->icon = ''; |
||
159 | $this->description = ''; |
||
160 | $this->status = self::STATUS_ENABLED; |
||
161 | $this->profiles = new ArrayCollection(); |
||
162 | } |
||
163 | |||
164 | public function __toString(): string |
||
165 | { |
||
166 | return $this->getTitle(); |
||
167 | } |
||
168 | |||
169 | public function setTitle(string $title): self |
||
170 | { |
||
171 | $this->title = $title; |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | public function getTitle(): string |
||
177 | { |
||
178 | return $this->title; |
||
179 | } |
||
180 | |||
181 | public function getShortCode(): string |
||
182 | { |
||
183 | return $this->shortCode; |
||
184 | } |
||
185 | |||
186 | public function setShortCode(string $shortCode): self |
||
187 | { |
||
188 | $this->shortCode = $shortCode; |
||
189 | |||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | public function setDescription(string $description): self |
||
194 | { |
||
195 | $this->description = $description; |
||
196 | |||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | public function getDescription(): string |
||
203 | } |
||
204 | |||
205 | public function setAccessUrlId(int $accessUrlId): static |
||
206 | { |
||
207 | $this->accessUrlId = $accessUrlId; |
||
208 | |||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | public function getAccessUrlId(): int |
||
213 | { |
||
214 | return $this->accessUrlId; |
||
215 | } |
||
216 | |||
217 | public function setIcon(string $icon): self |
||
218 | { |
||
219 | $this->icon = $icon; |
||
220 | |||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | public function getIcon(): string |
||
225 | { |
||
226 | return $this->icon; |
||
227 | } |
||
228 | |||
229 | public function setCriteria(string $criteria): self |
||
234 | } |
||
235 | |||
236 | public function getCriteria(): ?string |
||
237 | { |
||
238 | return $this->criteria; |
||
239 | } |
||
240 | |||
241 | public function setStatus(int $status): self |
||
242 | { |
||
243 | $this->status = $status; |
||
244 | |||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | public function getStatus(): int |
||
249 | { |
||
250 | return $this->status; |
||
251 | } |
||
252 | |||
253 | public function setUpdatedAt(DateTime $updatedAt): static |
||
254 | { |
||
255 | $this->updatedAt = $updatedAt; |
||
256 | |||
257 | return $this; |
||
258 | } |
||
259 | |||
260 | public function getUpdatedAt(): DateTime |
||
261 | { |
||
262 | return $this->updatedAt; |
||
263 | } |
||
264 | |||
265 | public function getId(): ?int |
||
266 | { |
||
267 | return $this->id; |
||
268 | } |
||
269 | |||
270 | public function getLevelProfile(): ?SkillLevelProfile |
||
271 | { |
||
272 | return $this->levelProfile; |
||
273 | } |
||
274 | |||
275 | public function setLevelProfile(SkillLevelProfile $levelProfile): self |
||
276 | { |
||
277 | $this->levelProfile = $levelProfile; |
||
278 | |||
279 | return $this; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @return Collection<int, SkillRelUser> |
||
284 | */ |
||
285 | public function getIssuedSkills(): Collection |
||
286 | { |
||
287 | return $this->issuedSkills; |
||
288 | } |
||
289 | |||
290 | public function addIssuedSkill(SkillRelUser $issuedSkill): static |
||
291 | { |
||
292 | if (!$this->issuedSkills->contains($issuedSkill)) { |
||
293 | $this->issuedSkills->add($issuedSkill); |
||
294 | $issuedSkill->setSkill($this); |
||
295 | } |
||
296 | |||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | public function removeIssuedSkill(SkillRelUser $issuedSkill): static |
||
301 | { |
||
302 | if ($this->issuedSkills->removeElement($issuedSkill)) { |
||
303 | // set the owning side to null (unless already changed) |
||
304 | if ($issuedSkill->getSkill() === $this) { |
||
305 | $issuedSkill->setSkill(null); |
||
306 | } |
||
307 | } |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @return Collection<int, SkillRelItem> |
||
314 | */ |
||
315 | public function getItems(): Collection |
||
316 | { |
||
317 | return $this->items; |
||
318 | } |
||
319 | |||
320 | public function setItems(ArrayCollection $items): self |
||
321 | { |
||
322 | $this->items = $items; |
||
323 | |||
324 | return $this; |
||
325 | } |
||
326 | |||
327 | public function hasItem(int $typeId, int $itemId): bool |
||
328 | { |
||
329 | if (0 !== $this->getItems()->count()) { |
||
330 | $found = false; |
||
331 | |||
332 | /** @var SkillRelItem $item */ |
||
333 | foreach ($this->getItems() as $item) { |
||
334 | if ($item->getItemId() === $itemId && $item->getItemType() === $typeId) { |
||
335 | $found = true; |
||
336 | |||
337 | break; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | return $found; |
||
342 | } |
||
343 | |||
344 | return false; |
||
345 | } |
||
346 | |||
347 | public function addItem(SkillRelItem $skillRelItem): void |
||
348 | { |
||
349 | $skillRelItem->setSkill($this); |
||
350 | $this->items[] = $skillRelItem; |
||
351 | } |
||
352 | |||
353 | public function getCourses(): Collection |
||
354 | { |
||
355 | return $this->courses; |
||
356 | } |
||
357 | |||
358 | public function setCourses(ArrayCollection $courses): self |
||
359 | { |
||
360 | $this->courses = $courses; |
||
361 | |||
362 | return $this; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @return Collection<int, SkillRelSkill> |
||
367 | */ |
||
368 | public function getSkills(): Collection |
||
369 | { |
||
370 | return $this->skills; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param Collection<int, SkillRelSkill> $skills |
||
375 | */ |
||
376 | public function setSkills(Collection $skills): self |
||
377 | { |
||
378 | $this->skills = $skills; |
||
379 | |||
380 | return $this; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @return Collection<int, SkillRelGradebook> |
||
385 | */ |
||
386 | public function getGradeBookCategories(): Collection |
||
387 | { |
||
388 | return $this->gradeBookCategories; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @param Collection<int, SkillRelGradebook> $gradeBookCategories |
||
393 | */ |
||
394 | public function setGradeBookCategories(Collection $gradeBookCategories): self |
||
395 | { |
||
396 | $this->gradeBookCategories = $gradeBookCategories; |
||
397 | |||
398 | return $this; |
||
399 | } |
||
400 | |||
401 | public function hasAsset(): bool |
||
402 | { |
||
403 | return null !== $this->asset; |
||
404 | } |
||
405 | |||
406 | public function getAsset(): ?Asset |
||
407 | { |
||
408 | return $this->asset; |
||
409 | } |
||
410 | |||
411 | public function setAsset(?Asset $asset): self |
||
412 | { |
||
413 | $this->asset = $asset; |
||
414 | |||
415 | return $this; |
||
416 | } |
||
417 | |||
418 | public function hasCourseAndSession(SkillRelCourse $searchItem): bool |
||
419 | { |
||
420 | if (0 !== $this->getCourses()->count()) { |
||
421 | $found = false; |
||
422 | |||
423 | /** @var SkillRelCourse $item */ |
||
424 | foreach ($this->getCourses() as $item) { |
||
425 | $sessionPassFilter = false; |
||
426 | $session = $item->getSession(); |
||
427 | $sessionId = empty($session) ? 0 : $session->getId(); |
||
428 | $searchSessionId = empty($searchItem->getSession()) ? 0 : $searchItem->getSession()->getId(); |
||
429 | if ($sessionId === $searchSessionId) { |
||
430 | $sessionPassFilter = true; |
||
431 | } |
||
432 | if ($item->getCourse()->getId() === $searchItem->getCourse()->getId() && $sessionPassFilter) { |
||
433 | $found = true; |
||
434 | |||
435 | break; |
||
436 | } |
||
437 | } |
||
438 | |||
439 | return $found; |
||
440 | } |
||
441 | |||
442 | return false; |
||
443 | } |
||
444 | |||
445 | public function addToCourse(SkillRelCourse $item): void |
||
446 | { |
||
447 | $item->setSkill($this); |
||
448 | $this->courses[] = $item; |
||
449 | } |
||
450 | |||
451 | public function getLocale(): string |
||
452 | { |
||
453 | return $this->locale; |
||
|
|||
454 | } |
||
455 | |||
456 | public function setLocale(string $locale): self |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @return Collection<int, Skill> |
||
465 | */ |
||
466 | public function getChildSkills(): Collection |
||
467 | { |
||
468 | return $this |
||
469 | ->getSkills() |
||
470 | ->map(fn(SkillRelSkill $skillRelSkill): Skill => $skillRelSkill->getSkill()) |
||
471 | ; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * @return Collection<int, SkillRelProfile> |
||
476 | */ |
||
477 | public function getProfiles(): Collection |
||
478 | { |
||
479 | return $this->profiles; |
||
480 | } |
||
481 | |||
482 | public function addProfile(SkillRelProfile $profile): static |
||
490 | } |
||
491 | |||
492 | public function removeProfile(SkillRelProfile $profile): static |
||
493 | { |
||
494 | if ($this->profiles->removeElement($profile)) { |
||
495 | // set the owning side to null (unless already changed) |
||
496 | if ($profile->getSkill() === $this) { |
||
497 | $profile->setSkill(null); |
||
498 | } |
||
499 | } |
||
502 | } |
||
503 | } |
||
504 |