Total Complexity | 50 |
Total Lines | 378 |
Duplicated Lines | 0 % |
Changes | 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 |
||
24 | #[ApiResource(normalizationContext: ['groups' => ['skill:read']], security: 'is_granted(\'ROLE_ADMIN\')')] |
||
25 | #[ApiFilter(SearchFilter::class, properties: ['issuedSkills.user' => 'exact'])] |
||
26 | #[ORM\Table(name: 'skill')] |
||
27 | #[ORM\Entity(repositoryClass: SkillRepository::class)] |
||
28 | class Skill implements Stringable, Translatable |
||
29 | { |
||
30 | public const STATUS_DISABLED = 0; |
||
31 | public const STATUS_ENABLED = 1; |
||
32 | |||
33 | #[Groups(['skill:read'])] |
||
34 | #[ORM\Column(name: 'id', type: 'integer')] |
||
35 | #[ORM\Id] |
||
36 | #[ORM\GeneratedValue] |
||
37 | protected ?int $id = null; |
||
38 | |||
39 | #[ORM\ManyToOne(targetEntity: Profile::class, inversedBy: 'skills')] |
||
40 | #[ORM\JoinColumn(name: 'profile_id', referencedColumnName: 'id')] |
||
41 | protected ?Profile $profile = null; |
||
42 | |||
43 | /** |
||
44 | * @var Collection<int, SkillRelUser> |
||
45 | */ |
||
46 | #[Groups(['skill:read'])] |
||
47 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelUser::class, cascade: ['persist'])] |
||
48 | protected Collection $issuedSkills; |
||
49 | |||
50 | /** |
||
51 | * @var Collection<int, SkillRelItem> |
||
52 | */ |
||
53 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelItem::class, cascade: ['persist'])] |
||
54 | protected Collection $items; |
||
55 | |||
56 | /** |
||
57 | * @var Collection<int, SkillRelSkill> |
||
58 | */ |
||
59 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelSkill::class, cascade: ['persist'])] |
||
60 | protected Collection $skills; |
||
61 | |||
62 | /** |
||
63 | * @var Collection<int, SkillRelCourse> |
||
64 | */ |
||
65 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelCourse::class, cascade: ['persist'])] |
||
66 | protected Collection $courses; |
||
67 | |||
68 | /** |
||
69 | * @var Collection<int, SkillRelGradebook> |
||
70 | */ |
||
71 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelGradebook::class, cascade: ['persist'])] |
||
72 | protected Collection $gradeBookCategories; |
||
73 | |||
74 | #[Gedmo\Translatable] |
||
75 | #[Assert\NotBlank] |
||
76 | #[Groups(['skill:read', 'skill:write', 'skill_rel_user:read'])] |
||
77 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
78 | protected string $title; |
||
79 | |||
80 | #[Gedmo\Translatable] |
||
81 | #[Assert\NotBlank] |
||
82 | #[Groups(['skill:read', 'skill:write'])] |
||
83 | #[ORM\Column(name: 'short_code', type: 'string', length: 100, nullable: false)] |
||
84 | protected string $shortCode; |
||
85 | |||
86 | #[Groups(['skill:read', 'skill:write'])] |
||
87 | #[ORM\Column(name: 'description', type: 'text', nullable: false)] |
||
88 | protected string $description; |
||
89 | |||
90 | #[Assert\NotNull] |
||
91 | #[ORM\Column(name: 'access_url_id', type: 'integer', nullable: false)] |
||
92 | protected int $accessUrlId; |
||
93 | |||
94 | #[Groups(['skill:read', 'skill_rel_user:read'])] |
||
95 | #[ORM\Column(name: 'icon', type: 'string', length: 255, nullable: false)] |
||
96 | protected string $icon; |
||
97 | |||
98 | #[ORM\ManyToOne(targetEntity: Asset::class, cascade: ['persist', 'remove'])] |
||
99 | #[ORM\JoinColumn(name: 'asset_id', referencedColumnName: 'id')] |
||
100 | protected ?Asset $asset = null; |
||
101 | |||
102 | #[ORM\Column(name: 'criteria', type: 'text', nullable: true)] |
||
103 | protected ?string $criteria = null; |
||
104 | |||
105 | #[ORM\Column(name: 'status', type: 'integer', nullable: false, options: ['default' => 1])] |
||
106 | protected int $status; |
||
107 | |||
108 | #[Gedmo\Timestampable(on: 'update')] |
||
109 | #[ORM\Column(name: 'updated_at', type: 'datetime', nullable: false)] |
||
110 | protected DateTime $updatedAt; |
||
111 | |||
112 | #[Gedmo\Locale] |
||
113 | private ?string $locale = null; |
||
114 | |||
115 | public function __construct() |
||
116 | { |
||
117 | $this->issuedSkills = new ArrayCollection(); |
||
118 | $this->items = new ArrayCollection(); |
||
119 | $this->courses = new ArrayCollection(); |
||
120 | $this->gradeBookCategories = new ArrayCollection(); |
||
121 | $this->skills = new ArrayCollection(); |
||
122 | $this->icon = ''; |
||
123 | $this->description = ''; |
||
124 | $this->status = self::STATUS_ENABLED; |
||
125 | } |
||
126 | |||
127 | public function __toString(): string |
||
130 | } |
||
131 | |||
132 | public function setTitle(string $title): self |
||
133 | { |
||
134 | $this->title = $title; |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | public function getTitle(): string |
||
140 | { |
||
141 | return $this->title; |
||
142 | } |
||
143 | |||
144 | public function getShortCode(): string |
||
145 | { |
||
146 | return $this->shortCode; |
||
147 | } |
||
148 | |||
149 | public function setShortCode(string $shortCode): self |
||
150 | { |
||
151 | $this->shortCode = $shortCode; |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | public function setDescription(string $description): self |
||
157 | { |
||
158 | $this->description = $description; |
||
159 | |||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | public function getDescription(): string |
||
164 | { |
||
165 | return $this->description; |
||
166 | } |
||
167 | |||
168 | public function setAccessUrlId(int $accessUrlId): static |
||
169 | { |
||
170 | $this->accessUrlId = $accessUrlId; |
||
171 | |||
172 | return $this; |
||
173 | } |
||
174 | |||
175 | public function getAccessUrlId(): int |
||
176 | { |
||
177 | return $this->accessUrlId; |
||
178 | } |
||
179 | |||
180 | public function setIcon(string $icon): self |
||
185 | } |
||
186 | |||
187 | public function getIcon(): string |
||
188 | { |
||
189 | return $this->icon; |
||
190 | } |
||
191 | |||
192 | public function setCriteria(string $criteria): self |
||
193 | { |
||
194 | $this->criteria = $criteria; |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | public function getCriteria(): ?string |
||
200 | { |
||
201 | return $this->criteria; |
||
202 | } |
||
203 | |||
204 | public function setStatus(int $status): self |
||
205 | { |
||
206 | $this->status = $status; |
||
207 | |||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | public function getStatus(): int |
||
212 | { |
||
213 | return $this->status; |
||
214 | } |
||
215 | |||
216 | public function setUpdatedAt(DateTime $updatedAt): static |
||
217 | { |
||
218 | $this->updatedAt = $updatedAt; |
||
219 | |||
220 | return $this; |
||
221 | } |
||
222 | |||
223 | public function getUpdatedAt(): DateTime |
||
224 | { |
||
225 | return $this->updatedAt; |
||
226 | } |
||
227 | |||
228 | public function getId(): ?int |
||
229 | { |
||
230 | return $this->id; |
||
231 | } |
||
232 | |||
233 | public function getProfile(): ?Profile |
||
234 | { |
||
235 | return $this->profile; |
||
236 | } |
||
237 | |||
238 | public function setProfile(Profile $profile): self |
||
239 | { |
||
240 | $this->profile = $profile; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @return Collection<int, SkillRelUser> |
||
247 | */ |
||
248 | public function getIssuedSkills(): Collection |
||
249 | { |
||
250 | return $this->issuedSkills; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @return Collection<int, SkillRelItem> |
||
255 | */ |
||
256 | public function getItems(): Collection |
||
257 | { |
||
258 | return $this->items; |
||
259 | } |
||
260 | |||
261 | public function setItems(ArrayCollection $items): self |
||
262 | { |
||
263 | $this->items = $items; |
||
264 | |||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | public function hasItem(int $typeId, int $itemId): bool |
||
269 | { |
||
270 | if (0 !== $this->getItems()->count()) { |
||
271 | $found = false; |
||
272 | |||
273 | /** @var SkillRelItem $item */ |
||
274 | foreach ($this->getItems() as $item) { |
||
275 | if ($item->getItemId() === $itemId && $item->getItemType() === $typeId) { |
||
276 | $found = true; |
||
277 | |||
278 | break; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | return $found; |
||
283 | } |
||
284 | |||
285 | return false; |
||
286 | } |
||
287 | |||
288 | public function addItem(SkillRelItem $skillRelItem): void |
||
289 | { |
||
290 | $skillRelItem->setSkill($this); |
||
291 | $this->items[] = $skillRelItem; |
||
292 | } |
||
293 | |||
294 | public function getCourses(): Collection |
||
295 | { |
||
296 | return $this->courses; |
||
297 | } |
||
298 | |||
299 | public function setCourses(ArrayCollection $courses): self |
||
300 | { |
||
301 | $this->courses = $courses; |
||
302 | |||
303 | return $this; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @return Collection<int, SkillRelSkill> |
||
308 | */ |
||
309 | public function getSkills(): Collection |
||
310 | { |
||
311 | return $this->skills; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param Collection<int, SkillRelSkill> $skills |
||
316 | */ |
||
317 | public function setSkills(Collection $skills): self |
||
318 | { |
||
319 | $this->skills = $skills; |
||
320 | |||
321 | return $this; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @return Collection<int, SkillRelGradebook> |
||
326 | */ |
||
327 | public function getGradeBookCategories(): Collection |
||
328 | { |
||
329 | return $this->gradeBookCategories; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param Collection<int, SkillRelGradebook> $gradeBookCategories |
||
334 | */ |
||
335 | public function setGradeBookCategories(Collection $gradeBookCategories): self |
||
336 | { |
||
337 | $this->gradeBookCategories = $gradeBookCategories; |
||
338 | |||
339 | return $this; |
||
340 | } |
||
341 | |||
342 | public function hasAsset(): bool |
||
343 | { |
||
344 | return null !== $this->asset; |
||
345 | } |
||
346 | |||
347 | public function getAsset(): ?Asset |
||
348 | { |
||
349 | return $this->asset; |
||
350 | } |
||
351 | |||
352 | public function setAsset(?Asset $asset): self |
||
353 | { |
||
354 | $this->asset = $asset; |
||
355 | |||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | public function hasCourseAndSession(SkillRelCourse $searchItem): bool |
||
360 | { |
||
361 | if (0 !== $this->getCourses()->count()) { |
||
362 | $found = false; |
||
363 | |||
364 | /** @var SkillRelCourse $item */ |
||
365 | foreach ($this->getCourses() as $item) { |
||
366 | $sessionPassFilter = false; |
||
367 | $session = $item->getSession(); |
||
368 | $sessionId = empty($session) ? 0 : $session->getId(); |
||
369 | $searchSessionId = empty($searchItem->getSession()) ? 0 : $searchItem->getSession()->getId(); |
||
370 | if ($sessionId === $searchSessionId) { |
||
371 | $sessionPassFilter = true; |
||
372 | } |
||
373 | if ($item->getCourse()->getId() === $searchItem->getCourse()->getId() && $sessionPassFilter) { |
||
374 | $found = true; |
||
375 | |||
376 | break; |
||
377 | } |
||
378 | } |
||
379 | |||
380 | return $found; |
||
381 | } |
||
382 | |||
383 | return false; |
||
384 | } |
||
385 | |||
386 | public function addToCourse(SkillRelCourse $item): void |
||
387 | { |
||
388 | $item->setSkill($this); |
||
389 | $this->courses[] = $item; |
||
390 | } |
||
391 | |||
392 | public function getLocale(): string |
||
393 | { |
||
394 | return $this->locale; |
||
|
|||
395 | } |
||
396 | |||
397 | public function setLocale(string $locale): Skill |
||
402 | } |
||
403 | } |
||
404 |