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