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