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