| Total Complexity | 49 |
| Total Lines | 505 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ResourceNode 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 ResourceNode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ResourceNode |
||
| 27 | { |
||
| 28 | use TimestampableEntity; |
||
| 29 | |||
| 30 | public const PATH_SEPARATOR = '`'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @ORM\Id |
||
| 34 | * @ORM\Column(type="integer") |
||
| 35 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 36 | */ |
||
| 37 | protected $id; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @Assert\NotBlank() |
||
| 41 | * |
||
| 42 | * @Gedmo\TreePathSource |
||
| 43 | * @ORM\Column(name="slug", type="string", length=255, nullable=true) |
||
| 44 | */ |
||
| 45 | protected $slug; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceType", inversedBy="resourceNodes") |
||
| 49 | * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id", nullable=false) |
||
| 50 | */ |
||
| 51 | protected $resourceType; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode", |
||
| 55 | * cascade={"remove"}) |
||
| 56 | */ |
||
| 57 | protected $resourceLinks; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ResourceFile |
||
| 61 | * |
||
| 62 | * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", orphanRemoval=true) |
||
| 63 | * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 64 | */ |
||
| 65 | protected $resourceFile; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @ORM\ManyToOne( |
||
| 69 | * targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="resourceNodes" |
||
| 70 | * ) |
||
| 71 | * @ORM\JoinColumn(name="creator_id", referencedColumnName="id", nullable=true, onDelete="CASCADE") |
||
| 72 | */ |
||
| 73 | protected $creator; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @Gedmo\TreeParent |
||
| 77 | * |
||
| 78 | * @ORM\ManyToOne( |
||
| 79 | * targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", |
||
| 80 | * inversedBy="children" |
||
| 81 | * ) |
||
| 82 | * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")}) |
||
| 83 | */ |
||
| 84 | protected $parent; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @Gedmo\TreeLevel |
||
| 88 | * |
||
| 89 | * @ORM\Column(name="level", type="integer", nullable=true) |
||
| 90 | */ |
||
| 91 | protected $level; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var ResourceNode[] |
||
| 95 | * |
||
| 96 | * @ORM\OneToMany( |
||
| 97 | * targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", |
||
| 98 | * mappedBy="parent" |
||
| 99 | * ) |
||
| 100 | * @ORM\OrderBy({"id" = "ASC"}) |
||
| 101 | */ |
||
| 102 | protected $children; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @Gedmo\TreePath(separator="`") |
||
| 106 | * |
||
| 107 | * @ORM\Column(name="path", type="string", length=3000, nullable=true) |
||
| 108 | */ |
||
| 109 | protected $path; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Shortcut to access Course resource from ResourceNode. |
||
| 113 | * |
||
| 114 | * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", mappedBy="resourceNode") |
||
| 115 | */ |
||
| 116 | protected $course; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Shortcut to access Course resource from ResourceNode. |
||
| 120 | * |
||
| 121 | * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Illustration", mappedBy="resourceNode") |
||
| 122 | */ |
||
| 123 | protected $illustration; |
||
| 124 | |||
| 125 | //protected $pathForCreationLog = ''; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Constructor. |
||
| 129 | */ |
||
| 130 | public function __construct() |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function __toString() |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return Course |
||
| 145 | */ |
||
| 146 | public function getCourse(): ?Course |
||
| 147 | { |
||
| 148 | return $this->course; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function isCourseNode(): bool |
||
| 152 | { |
||
| 153 | return null !== $this->course; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function isIllustrationNode(): bool |
||
| 157 | { |
||
| 158 | return null !== $this->illustration; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the resource id. |
||
| 163 | * |
||
| 164 | * @return int |
||
| 165 | */ |
||
| 166 | public function getId() |
||
| 167 | { |
||
| 168 | return $this->id; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param int $id |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function setId($id) |
||
| 177 | { |
||
| 178 | $this->id = $id; |
||
| 179 | |||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns the resource creator. |
||
| 185 | * |
||
| 186 | * @return User |
||
| 187 | */ |
||
| 188 | public function getCreator(): ?User |
||
| 189 | { |
||
| 190 | return $this->creator; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function setCreator(User $creator = null) |
||
| 194 | { |
||
| 195 | $this->creator = $creator; |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the children resource instances. |
||
| 202 | * |
||
| 203 | * @return ArrayCollection |
||
| 204 | */ |
||
| 205 | public function getChildren() |
||
| 206 | { |
||
| 207 | return $this->children; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Sets the parent resource. |
||
| 212 | * |
||
| 213 | * @param ResourceNode $parent |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function setParent(ResourceNode $parent = null) |
||
| 218 | { |
||
| 219 | $this->parent = $parent; |
||
| 220 | |||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the parent resource. |
||
| 226 | * |
||
| 227 | * @return ResourceNode |
||
| 228 | */ |
||
| 229 | public function getParent() |
||
| 230 | { |
||
| 231 | return $this->parent; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return the lvl value of the resource in the tree. |
||
| 236 | * |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | public function getLevel() |
||
| 240 | { |
||
| 241 | return $this->level; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the "raw" path of the resource |
||
| 246 | * (the path merge names and ids of all items). |
||
| 247 | * Eg.: "Root-1/subdir-2/file.txt-3/". |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getPath() |
||
| 252 | { |
||
| 253 | return $this->path; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the path cleaned from its ids. |
||
| 258 | * Eg.: "Root/subdir/file.txt". |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getPathForDisplay() |
||
| 263 | { |
||
| 264 | return self::convertPathForDisplay($this->path); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getPathForDisplayRemoveBase(string $base) |
||
| 271 | { |
||
| 272 | $path = str_replace($base, '', $this->path); |
||
| 273 | |||
| 274 | return self::convertPathForDisplay($path); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | public function getSlug() |
||
| 281 | { |
||
| 282 | return $this->slug; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param mixed $slug |
||
| 287 | * |
||
| 288 | * @return ResourceNode |
||
| 289 | */ |
||
| 290 | public function setSlug($slug) |
||
| 291 | { |
||
| 292 | if (strpos(self::PATH_SEPARATOR, $slug) !== false) { |
||
| 293 | throw new \InvalidArgumentException('Invalid character "'.self::PATH_SEPARATOR.'" in resource name.'); |
||
| 294 | } |
||
| 295 | |||
| 296 | $this->slug = $slug; |
||
| 297 | |||
| 298 | return $this; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Convert a path for display: remove ids. |
||
| 303 | * |
||
| 304 | * @param string $path |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public static function convertPathForDisplay($path) |
||
| 309 | { |
||
| 310 | /*$pathForDisplay = preg_replace( |
||
| 311 | '/-\d+'.self::PATH_SEPARATOR.'/', |
||
| 312 | ' / ', |
||
| 313 | $path |
||
| 314 | ); |
||
| 315 | if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) { |
||
| 316 | $pathForDisplay = substr_replace($pathForDisplay, '', -3); |
||
| 317 | } |
||
| 318 | */ |
||
| 319 | $pathForDisplay = preg_replace( |
||
| 320 | '/-\d+'.self::PATH_SEPARATOR.'/', |
||
| 321 | '/', |
||
| 322 | $path |
||
| 323 | ); |
||
| 324 | |||
| 325 | if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) { |
||
| 326 | $pathForDisplay = substr_replace($pathForDisplay, '', -1); |
||
| 327 | } |
||
| 328 | |||
| 329 | return $pathForDisplay; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * This is required for logging the resource path at the creation. |
||
| 334 | * Do not use this function otherwise. |
||
| 335 | */ |
||
| 336 | public function setPathForCreationLog($path) |
||
| 337 | { |
||
| 338 | $this->pathForCreationLog = $path; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * This is required for logging the resource path at the creation. |
||
| 343 | * Do not use this function otherwise. |
||
| 344 | * |
||
| 345 | * @return type |
||
| 346 | */ |
||
| 347 | public function getPathForCreationLog() |
||
| 348 | { |
||
| 349 | return $this->pathForCreationLog; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return ResourceType |
||
| 354 | */ |
||
| 355 | public function getResourceType() |
||
| 356 | { |
||
| 357 | return $this->resourceType; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param ResourceType $resourceType |
||
| 362 | * |
||
| 363 | * @return ResourceNode |
||
| 364 | */ |
||
| 365 | public function setResourceType($resourceType) |
||
| 366 | { |
||
| 367 | $this->resourceType = $resourceType; |
||
| 368 | |||
| 369 | return $this; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return ArrayCollection|ResourceLink[] |
||
| 374 | */ |
||
| 375 | public function getResourceLinks() |
||
| 376 | { |
||
| 377 | return $this->resourceLinks; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param mixed $resourceLinks |
||
| 382 | * |
||
| 383 | * @return ResourceNode |
||
| 384 | */ |
||
| 385 | public function setResourceLinks($resourceLinks) |
||
| 386 | { |
||
| 387 | $this->resourceLinks = $resourceLinks; |
||
| 388 | |||
| 389 | return $this; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param Session $session |
||
| 394 | * |
||
| 395 | * @return ArrayCollection |
||
| 396 | */ |
||
| 397 | public function hasSession(Session $session = null) |
||
| 398 | { |
||
| 399 | $links = $this->getResourceLinks(); |
||
| 400 | $criteria = Criteria::create(); |
||
| 401 | |||
| 402 | $criteria->andWhere( |
||
| 403 | Criteria::expr()->eq('session', $session) |
||
| 404 | ); |
||
| 405 | |||
| 406 | $result = $links->matching($criteria); |
||
| 407 | |||
| 408 | return $result; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | public function hasResourceFile() |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return ResourceFile |
||
| 421 | */ |
||
| 422 | public function getResourceFile(): ?ResourceFile |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | public function hasEditableContent() |
||
| 431 | { |
||
| 432 | if ($this->hasResourceFile()) { |
||
| 433 | $mimeType = $this->getResourceFile()->getMimeType(); |
||
| 434 | if (strpos($mimeType, 'text') !== false) { |
||
| 435 | return true; |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | return false; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | public function isResourceFileAnImage() |
||
| 446 | { |
||
| 447 | if ($this->hasResourceFile()) { |
||
| 448 | $mimeType = $this->getResourceFile()->getMimeType(); |
||
| 449 | if (strpos($mimeType, 'image') !== false) { |
||
| 450 | return true; |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | return false; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function isResourceFileAVideo() |
||
| 470 | } |
||
| 471 | |||
| 472 | public function setResourceFile(ResourceFile $resourceFile): ResourceNode |
||
| 473 | { |
||
| 474 | $this->resourceFile = $resourceFile; |
||
| 475 | |||
| 476 | return $this; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public function getIcon() |
||
| 483 | { |
||
| 484 | $class = 'fa fa-folder'; |
||
| 485 | if ($this->hasResourceFile()) { |
||
| 486 | $class = 'far fa-file'; |
||
| 487 | |||
| 488 | if ($this->isResourceFileAnImage()) { |
||
| 489 | $class = 'far fa-file-image'; |
||
| 490 | } |
||
| 491 | if ($this->isResourceFileAVideo()) { |
||
| 492 | $class = 'far fa-file-video'; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | return '<i class="'.$class.'"></i>'; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | public function getThumbnail(RouterInterface $router) |
||
| 531 | } |
||
| 532 | |||
| 533 | } |
||
| 534 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..