Total Complexity | 47 |
Total Lines | 463 |
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 |
||
20 | class Skill |
||
21 | { |
||
22 | const STATUS_DISABLED = 0; |
||
23 | const STATUS_ENABLED = 1; |
||
24 | |||
25 | /** |
||
26 | * @ORM\ManyToOne(targetEntity="Chamilo\SkillBundle\Entity\Profile", inversedBy="skills") |
||
27 | * @ORM\JoinColumn(name="profile_id", referencedColumnName="id") |
||
28 | */ |
||
29 | protected $profile; |
||
30 | |||
31 | /** |
||
32 | * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="skill", cascade={"persist"}) |
||
33 | */ |
||
34 | protected $issuedSkills; |
||
35 | |||
36 | /** |
||
37 | * // uncomment if api_get_configuration_value('allow_skill_rel_items') |
||
38 | * ORM\OneToMany(targetEntity="Chamilo\SkillBundle\Entity\SkillRelItem", mappedBy="skill", cascade={"persist"}). |
||
39 | */ |
||
40 | protected $items; |
||
41 | |||
42 | /** |
||
43 | * // uncomment if api_get_configuration_value('allow_skill_rel_items') |
||
44 | * ORM\OneToMany(targetEntity="Chamilo\SkillBundle\Entity\SkillRelCourse", mappedBy="skill", cascade={"persist"}). |
||
45 | */ |
||
46 | protected $courses; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | * |
||
51 | * @ORM\Column(name="id", type="integer") |
||
52 | * @ORM\Id |
||
53 | * @ORM\GeneratedValue |
||
54 | */ |
||
55 | protected $id; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | * |
||
60 | * @ORM\Column(name="name", type="string", length=255, nullable=false) |
||
61 | */ |
||
62 | protected $name; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | * |
||
67 | * @ORM\Column(name="short_code", type="string", length=100, nullable=false) |
||
68 | */ |
||
69 | protected $shortCode; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | * |
||
74 | * @ORM\Column(name="description", type="text", nullable=false) |
||
75 | */ |
||
76 | protected $description; |
||
77 | |||
78 | /** |
||
79 | * @var int |
||
80 | * |
||
81 | * @ORM\Column(name="access_url_id", type="integer", nullable=false) |
||
82 | */ |
||
83 | protected $accessUrlId; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | * |
||
88 | * @ORM\Column(name="icon", type="string", length=255, nullable=false) |
||
89 | */ |
||
90 | protected $icon; |
||
91 | |||
92 | /** |
||
93 | * @var string |
||
94 | * |
||
95 | * @ORM\Column(name="criteria", type="text", nullable=true) |
||
96 | */ |
||
97 | protected $criteria; |
||
98 | |||
99 | /** |
||
100 | * @var int |
||
101 | * |
||
102 | * @ORM\Column(name="status", type="integer", nullable=false, options={"default": 1}) |
||
103 | */ |
||
104 | protected $status; |
||
105 | |||
106 | /** |
||
107 | * @var \DateTime |
||
108 | * |
||
109 | * @ORM\Column(name="updated_at", type="datetime", nullable=false) |
||
110 | */ |
||
111 | protected $updatedAt; |
||
112 | |||
113 | public function __construct() |
||
114 | { |
||
115 | $this->courses = new ArrayCollection(); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function __toString() |
||
122 | { |
||
123 | return (string) $this->getName(); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Set name. |
||
128 | * |
||
129 | * @param string $name |
||
130 | * |
||
131 | * @return Skill |
||
132 | */ |
||
133 | public function setName($name) |
||
134 | { |
||
135 | $this->name = $name; |
||
136 | |||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get name. |
||
142 | * |
||
143 | * @param bool $translated Optional. Get the name translated when is it exists in a sub-language. By default is true |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getName($translated = true) |
||
148 | { |
||
149 | if ($translated) { |
||
150 | $variable = ChamiloApi::getLanguageVar($this->name, 'Skill'); |
||
151 | |||
152 | return isset($GLOBALS[$variable]) ? $GLOBALS[$variable] : $this->name; |
||
153 | } |
||
154 | |||
155 | return $this->name; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Set shortCode. |
||
160 | * |
||
161 | * @param string $shortCode |
||
162 | * |
||
163 | * @return Skill |
||
164 | */ |
||
165 | public function setShortCode($shortCode) |
||
166 | { |
||
167 | $this->shortCode = $shortCode; |
||
168 | |||
169 | return $this; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Get shortCode. |
||
174 | * |
||
175 | * @param bool $translated Optional. Get the code translated when is it exists in a sub-language. By default is true |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getShortCode($translated = true) |
||
180 | { |
||
181 | if ($translated && !empty($this->shortCode)) { |
||
182 | $variable = ChamiloApi::getLanguageVar($this->shortCode, 'SkillCode'); |
||
183 | |||
184 | return isset($GLOBALS[$variable]) ? $GLOBALS[$variable] : $this->shortCode; |
||
185 | } |
||
186 | |||
187 | return $this->shortCode; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Set description. |
||
192 | * |
||
193 | * @param string $description |
||
194 | * |
||
195 | * @return Skill |
||
196 | */ |
||
197 | public function setDescription($description) |
||
198 | { |
||
199 | $this->description = $description; |
||
200 | |||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get description. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getDescription() |
||
210 | { |
||
211 | return $this->description; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Set accessUrlId. |
||
216 | * |
||
217 | * @param int $accessUrlId |
||
218 | * |
||
219 | * @return Skill |
||
220 | */ |
||
221 | public function setAccessUrlId($accessUrlId) |
||
222 | { |
||
223 | $this->accessUrlId = $accessUrlId; |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Get accessUrlId. |
||
230 | * |
||
231 | * @return int |
||
232 | */ |
||
233 | public function getAccessUrlId() |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Set icon. |
||
240 | * |
||
241 | * @param string $icon |
||
242 | * |
||
243 | * @return Skill |
||
244 | */ |
||
245 | public function setIcon($icon) |
||
246 | { |
||
247 | $this->icon = $icon; |
||
248 | |||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get icon. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getIcon() |
||
258 | { |
||
259 | return $this->icon; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Set criteria. |
||
264 | * |
||
265 | * @param string $criteria |
||
266 | * |
||
267 | * @return Skill |
||
268 | */ |
||
269 | public function setCriteria($criteria) |
||
270 | { |
||
271 | $this->criteria = $criteria; |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Get criteria. |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getCriteria() |
||
282 | { |
||
283 | return $this->criteria; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Set status. |
||
288 | * |
||
289 | * @param int $status |
||
290 | * |
||
291 | * @return Skill |
||
292 | */ |
||
293 | public function setStatus($status) |
||
294 | { |
||
295 | $this->status = $status; |
||
296 | |||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Get status. |
||
302 | * |
||
303 | * @return int |
||
304 | */ |
||
305 | public function getStatus() |
||
306 | { |
||
307 | return $this->status; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Set updatedAt. |
||
312 | * |
||
313 | * @param \DateTime $updatedAt The update datetime |
||
314 | * |
||
315 | * @return Skill |
||
316 | */ |
||
317 | public function setUpdatedAt(\DateTime $updatedAt) |
||
318 | { |
||
319 | $this->updatedAt = $updatedAt; |
||
320 | |||
321 | return $this; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Get updatedAt. |
||
326 | * |
||
327 | * @return \DateTime |
||
328 | */ |
||
329 | public function getUpdatedAt() |
||
330 | { |
||
331 | return $this->updatedAt; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Get id. |
||
336 | * |
||
337 | * @return int |
||
338 | */ |
||
339 | public function getId() |
||
340 | { |
||
341 | return $this->id; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @return Profile |
||
346 | */ |
||
347 | public function getProfile() |
||
348 | { |
||
349 | return $this->profile; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param Profile $profile |
||
354 | * |
||
355 | * @return Skill |
||
356 | */ |
||
357 | public function setProfile($profile) |
||
358 | { |
||
359 | $this->profile = $profile; |
||
360 | |||
361 | return $this; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Get issuedSkills. |
||
366 | * |
||
367 | * @return ArrayCollection |
||
368 | */ |
||
369 | public function getIssuedSkills() |
||
370 | { |
||
371 | return $this->issuedSkills; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @return ArrayCollection |
||
376 | */ |
||
377 | public function getItems() |
||
378 | { |
||
379 | return $this->items; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * @param ArrayCollection $items |
||
384 | * |
||
385 | * @return Skill |
||
386 | */ |
||
387 | public function setItems($items) |
||
388 | { |
||
389 | $this->items = $items; |
||
390 | |||
391 | return $this; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @param int $itemId |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function hasItem($typeId, $itemId) |
||
400 | { |
||
401 | if ($this->getItems()->count()) { |
||
402 | $found = false; |
||
403 | /** @var SkillRelItem $item */ |
||
404 | foreach ($this->getItems() as $item) { |
||
405 | if ($item->getItemId() == $itemId && $item->getItemType() == $typeId) { |
||
406 | $found = true; |
||
407 | break; |
||
408 | } |
||
409 | } |
||
410 | |||
411 | return $found; |
||
412 | } |
||
413 | |||
414 | return false; |
||
415 | } |
||
416 | |||
417 | public function addItem(SkillRelItem $skillRelItem) |
||
418 | { |
||
419 | $skillRelItem->setSkill($this); |
||
420 | $this->items[] = $skillRelItem; |
||
421 | } |
||
422 | |||
423 | public function hasCourses() |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @return ArrayCollection |
||
430 | */ |
||
431 | public function getCourses() |
||
432 | { |
||
433 | return $this->courses; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @param ArrayCollection $courses |
||
438 | * |
||
439 | * @return Skill |
||
440 | */ |
||
441 | public function setCourses($courses) |
||
442 | { |
||
443 | $this->courses = $courses; |
||
444 | |||
445 | return $this; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * @return bool |
||
450 | */ |
||
451 | public function hasCourseAndSession(SkillRelCourse $searchItem) |
||
452 | { |
||
453 | if ($this->getCourses()->count()) { |
||
454 | $found = false; |
||
455 | /** @var SkillRelCourse $item */ |
||
456 | foreach ($this->getCourses() as $item) { |
||
457 | $sessionPassFilter = false; |
||
458 | $session = $item->getSession(); |
||
459 | $sessionId = !empty($session) ? $session->getId() : 0; |
||
460 | $searchSessionId = !empty($searchItem->getSession()) ? $searchItem->getSession()->getId() : 0; |
||
461 | |||
462 | if ($sessionId === $searchSessionId) { |
||
463 | $sessionPassFilter = true; |
||
464 | } |
||
465 | if ($item->getCourse()->getId() == $searchItem->getCourse()->getId() && |
||
466 | $sessionPassFilter |
||
467 | ) { |
||
468 | $found = true; |
||
469 | break; |
||
470 | } |
||
471 | } |
||
472 | |||
473 | return $found; |
||
474 | } |
||
475 | |||
476 | return false; |
||
477 | } |
||
478 | |||
479 | public function addToCourse(SkillRelCourse $item) |
||
483 | } |
||
484 | } |
||
485 |