| Total Complexity | 71 |
| Total Lines | 588 |
| 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 |
||
| 45 | #[ORM\Table(name: 'resource_node')] |
||
| 46 | #[ORM\Entity(repositoryClass: ResourceNodeRepository::class)] |
||
| 47 | #[ORM\HasLifecycleCallbacks] |
||
| 48 | #[ORM\EntityListeners([ResourceNodeListener::class])] |
||
| 49 | #[Gedmo\Tree(type: 'materializedPath')] |
||
| 50 | #[ApiResource( |
||
| 51 | operations: [ |
||
| 52 | new Get(), |
||
| 53 | new Put(), |
||
| 54 | new Patch(), |
||
| 55 | new Delete(), |
||
| 56 | new GetCollection(), |
||
| 57 | ], |
||
| 58 | normalizationContext: [ |
||
| 59 | 'groups' => [ |
||
| 60 | 'resource_node:read', |
||
| 61 | 'document:read', |
||
| 62 | ], |
||
| 63 | ], |
||
| 64 | denormalizationContext: [ |
||
| 65 | 'groups' => [ |
||
| 66 | 'resource_node:write', |
||
| 67 | 'document:write', |
||
| 68 | ], |
||
| 69 | ] |
||
| 70 | )] |
||
| 71 | #[ApiFilter(filterClass: OrderFilter::class, properties: ['id', 'title', 'resourceFile', 'createdAt', 'updatedAt'])] |
||
| 72 | #[ApiFilter(filterClass: PropertyFilter::class)] |
||
| 73 | #[ApiFilter(filterClass: SearchFilter::class, properties: ['title' => 'partial'])] |
||
| 74 | class ResourceNode implements Stringable |
||
| 75 | { |
||
| 76 | use TimestampableAgoTrait; |
||
| 77 | use TimestampableTypedEntity; |
||
| 78 | |||
| 79 | public const PATH_SEPARATOR = '/'; |
||
| 80 | |||
| 81 | #[Groups(['resource_node:read', 'document:read', 'ctool:read', 'user_json:read', 'course:read'])] |
||
| 82 | #[ORM\Id] |
||
| 83 | #[ORM\Column(type: 'integer')] |
||
| 84 | #[ORM\GeneratedValue(strategy: 'AUTO')] |
||
| 85 | protected ?int $id = null; |
||
| 86 | |||
| 87 | #[Groups(['resource_node:read', 'resource_node:write', 'document:read', 'document:write'])] |
||
| 88 | #[Assert\NotBlank] |
||
| 89 | #[Gedmo\TreePathSource] |
||
| 90 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
| 91 | protected string $title; |
||
| 92 | |||
| 93 | #[Assert\NotBlank] |
||
| 94 | #[Gedmo\Slug(fields: ['title'])] |
||
| 95 | #[ORM\Column(name: 'slug', type: 'string', length: 255, nullable: false)] |
||
| 96 | protected string $slug; |
||
| 97 | |||
| 98 | #[Groups(['resource_node:read'])] |
||
| 99 | #[Assert\NotNull] |
||
| 100 | #[ORM\ManyToOne(targetEntity: ResourceType::class, inversedBy: 'resourceNodes')] |
||
| 101 | #[ORM\JoinColumn(name: 'resource_type_id', referencedColumnName: 'id', nullable: false)] |
||
| 102 | protected ResourceType $resourceType; |
||
| 103 | |||
| 104 | #[ORM\ManyToOne(targetEntity: ResourceFormat::class, inversedBy: 'resourceNodes')] |
||
| 105 | #[ORM\JoinColumn(name: 'resource_format_id', referencedColumnName: 'id')] |
||
| 106 | protected ?ResourceFormat $resourceFormat = null; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var Collection<int, ResourceLink> |
||
| 110 | */ |
||
| 111 | #[Groups(['ctool:read', 'c_tool_intro:read'])] |
||
| 112 | #[ORM\OneToMany(mappedBy: 'resourceNode', targetEntity: ResourceLink::class, cascade: ['persist', 'remove'])] |
||
| 113 | protected Collection $resourceLinks; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * ResourceFile available file for this node. |
||
| 117 | */ |
||
| 118 | #[Groups(['resource_node:read', 'resource_node:write', 'document:read', 'document:write', 'message:read'])] |
||
| 119 | #[ORM\OneToOne(inversedBy: 'resourceNode', targetEntity: ResourceFile::class, orphanRemoval: true)] |
||
| 120 | #[ORM\JoinColumn(name: 'resource_file_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
||
| 121 | protected ?ResourceFile $resourceFile = null; |
||
| 122 | |||
| 123 | #[Assert\NotNull] |
||
| 124 | #[Groups(['resource_node:read', 'resource_node:write', 'document:write'])] |
||
| 125 | #[ORM\ManyToOne(targetEntity: User::class, cascade: ['persist'], inversedBy: 'resourceNodes')] |
||
| 126 | #[ORM\JoinColumn(name: 'creator_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] |
||
| 127 | protected ?User $creator; |
||
| 128 | |||
| 129 | #[Groups(['resource_node:read'])] |
||
| 130 | #[MaxDepth(1)] |
||
| 131 | #[ORM\JoinColumn(name: 'parent_id', onDelete: 'CASCADE')] |
||
| 132 | #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
||
| 133 | #[Gedmo\TreeParent] |
||
| 134 | protected ?ResourceNode $parent = null; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var Collection<int, ResourceNode> |
||
| 138 | */ |
||
| 139 | #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class, cascade: ['persist'])] |
||
| 140 | #[ORM\OrderBy(['id' => 'ASC'])] |
||
| 141 | protected Collection $children; |
||
| 142 | |||
| 143 | #[Gedmo\TreeLevel] |
||
| 144 | #[ORM\Column(name: 'level', type: 'integer', nullable: true)] |
||
| 145 | protected ?int $level = null; |
||
| 146 | |||
| 147 | #[Groups(['resource_node:read', 'document:read'])] |
||
| 148 | #[Gedmo\TreePath(separator: '/', appendId: true)] |
||
| 149 | #[ORM\Column(name: 'path', type: 'text', nullable: true)] |
||
| 150 | protected ?string $path = null; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Shortcut to access Course resource from ResourceNode. |
||
| 154 | * Groups({"resource_node:read", "course:read"}). |
||
| 155 | * |
||
| 156 | * ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Illustration", mappedBy="resourceNode") |
||
| 157 | */ |
||
| 158 | // protected $illustration; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var Collection<int, ResourceComment> |
||
| 162 | */ |
||
| 163 | #[ORM\OneToMany(mappedBy: 'resourceNode', targetEntity: ResourceComment::class, cascade: ['persist', 'remove'])] |
||
| 164 | protected Collection $comments; |
||
| 165 | |||
| 166 | #[Groups(['resource_node:read', 'document:read'])] |
||
| 167 | #[Gedmo\Timestampable(on: 'create')] |
||
| 168 | #[ORM\Column(type: 'datetime')] |
||
| 169 | protected DateTime $createdAt; |
||
| 170 | |||
| 171 | #[Groups(['resource_node:read', 'document:read'])] |
||
| 172 | #[Gedmo\Timestampable(on: 'update')] |
||
| 173 | #[ORM\Column(type: 'datetime')] |
||
| 174 | protected DateTime $updatedAt; |
||
| 175 | |||
| 176 | #[Groups(['resource_node:read', 'document:read'])] |
||
| 177 | protected bool $fileEditableText; |
||
| 178 | |||
| 179 | #[Groups(['resource_node:read', 'document:read'])] |
||
| 180 | #[ORM\Column(type: 'boolean')] |
||
| 181 | protected bool $public; |
||
| 182 | |||
| 183 | protected ?string $content = null; |
||
| 184 | |||
| 185 | #[ORM\OneToOne(mappedBy: 'shortCutNode', targetEntity: CShortcut::class, cascade: ['persist', 'remove'])] |
||
| 186 | protected ?CShortcut $shortCut = null; |
||
| 187 | |||
| 188 | #[Groups(['resource_node:read', 'document:read'])] |
||
| 189 | #[ORM\Column(type: 'uuid', unique: true)] |
||
| 190 | protected ?UuidV4 $uuid = null; |
||
| 191 | |||
| 192 | public function __construct() |
||
| 193 | { |
||
| 194 | $this->public = false; |
||
| 195 | $this->uuid = Uuid::v4(); |
||
| 196 | $this->children = new ArrayCollection(); |
||
| 197 | $this->resourceLinks = new ArrayCollection(); |
||
| 198 | $this->comments = new ArrayCollection(); |
||
| 199 | $this->createdAt = new DateTime(); |
||
| 200 | $this->fileEditableText = false; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function __toString(): string |
||
| 204 | { |
||
| 205 | return $this->getPathForDisplay(); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns the path cleaned from its ids. |
||
| 210 | * Eg.: "Root/subdir/file.txt". |
||
| 211 | */ |
||
| 212 | public function getPathForDisplay(): string |
||
| 213 | { |
||
| 214 | return $this->path; |
||
|
|
|||
| 215 | // return $this->convertPathForDisplay($this->path); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getUuid(): ?UuidV4 |
||
| 219 | { |
||
| 220 | return $this->uuid; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function hasCreator(): bool |
||
| 224 | { |
||
| 225 | return null !== $this->creator; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getCreator(): ?User |
||
| 229 | { |
||
| 230 | return $this->creator; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function setCreator(?User $creator): self |
||
| 234 | { |
||
| 235 | $this->creator = $creator; |
||
| 236 | |||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the children resource instances. |
||
| 242 | * |
||
| 243 | * @return Collection<int, ResourceNode> |
||
| 244 | */ |
||
| 245 | public function getChildren(): Collection |
||
| 246 | { |
||
| 247 | return $this->children; |
||
| 248 | } |
||
| 249 | |||
| 250 | public function addChild(self $resourceNode): static |
||
| 251 | { |
||
| 252 | if (!$this->children->contains($resourceNode)) { |
||
| 253 | $this->children->add($resourceNode); |
||
| 254 | |||
| 255 | $resourceNode->setParent($this); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the parent resource. |
||
| 263 | */ |
||
| 264 | public function getParent(): ?self |
||
| 265 | { |
||
| 266 | return $this->parent; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Sets the parent resource. |
||
| 271 | */ |
||
| 272 | public function setParent(?self $parent = null): self |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the lvl value of the resource in the tree. |
||
| 281 | */ |
||
| 282 | public function getLevel(): int |
||
| 283 | { |
||
| 284 | return $this->level; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the "raw" path of the resource |
||
| 289 | * (the path merge names and ids of all items). |
||
| 290 | * Eg.: "Root-1/subdir-2/file.txt-3/". |
||
| 291 | */ |
||
| 292 | public function getPath(): ?string |
||
| 293 | { |
||
| 294 | return $this->path; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return Collection|ResourceComment[] |
||
| 299 | */ |
||
| 300 | public function getComments(): array|Collection |
||
| 303 | } |
||
| 304 | |||
| 305 | public function addComment(ResourceComment $comment): self |
||
| 306 | { |
||
| 307 | $comment->setResourceNode($this); |
||
| 308 | $this->comments->add($comment); |
||
| 309 | |||
| 310 | return $this; |
||
| 311 | } |
||
| 312 | |||
| 313 | public function getPathForDisplayToArray(?int $baseRoot = null): array |
||
| 314 | { |
||
| 315 | $parts = explode(self::PATH_SEPARATOR, $this->path); |
||
| 316 | $list = []; |
||
| 317 | foreach ($parts as $part) { |
||
| 318 | $parts = explode('-', $part); |
||
| 319 | if (empty($parts[1])) { |
||
| 320 | continue; |
||
| 321 | } |
||
| 322 | $value = $parts[0]; |
||
| 323 | $id = $parts[1]; |
||
| 324 | if (!empty($baseRoot) && $id < $baseRoot) { |
||
| 325 | continue; |
||
| 326 | } |
||
| 327 | $list[$id] = $value; |
||
| 328 | } |
||
| 329 | |||
| 330 | return $list; |
||
| 331 | } |
||
| 332 | |||
| 333 | public function getPathForDisplayRemoveBase(string $base): string |
||
| 334 | { |
||
| 335 | $path = str_replace($base, '', $this->path); |
||
| 336 | |||
| 337 | return $this->convertPathForDisplay($path); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Convert a path for display: remove ids. |
||
| 342 | */ |
||
| 343 | public function convertPathForDisplay(string $path): string |
||
| 344 | { |
||
| 345 | /*$pathForDisplay = preg_replace( |
||
| 346 | '/-\d+'.self::PATH_SEPARATOR.'/', |
||
| 347 | ' / ', |
||
| 348 | $path |
||
| 349 | ); |
||
| 350 | if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) { |
||
| 351 | $pathForDisplay = substr_replace($pathForDisplay, '', -3); |
||
| 352 | } |
||
| 353 | */ |
||
| 354 | $pathForDisplay = preg_replace('/-\\d+\\'.self::PATH_SEPARATOR.'/', '/', $path); |
||
| 355 | if (null !== $pathForDisplay && '' !== $pathForDisplay) { |
||
| 356 | $pathForDisplay = substr_replace($pathForDisplay, '', -1); |
||
| 357 | } |
||
| 358 | |||
| 359 | return $pathForDisplay; |
||
| 360 | } |
||
| 361 | |||
| 362 | public function getSlug(): string |
||
| 363 | { |
||
| 364 | return $this->slug; |
||
| 365 | } |
||
| 366 | |||
| 367 | public function setSlug(string $slug): self |
||
| 368 | { |
||
| 369 | if (str_contains(self::PATH_SEPARATOR, $slug)) { |
||
| 370 | $message = 'Invalid character "'.self::PATH_SEPARATOR.'" in resource name'; |
||
| 371 | |||
| 372 | throw new InvalidArgumentException($message); |
||
| 373 | } |
||
| 374 | $this->slug = $slug; |
||
| 375 | |||
| 376 | return $this; |
||
| 377 | } |
||
| 378 | |||
| 379 | public function getTitle(): string |
||
| 380 | { |
||
| 381 | return $this->title; |
||
| 382 | } |
||
| 383 | |||
| 384 | public function setTitle(string $title): self |
||
| 385 | { |
||
| 386 | $title = str_replace('/', '-', $title); |
||
| 387 | $this->title = $title; |
||
| 388 | |||
| 389 | return $this; |
||
| 390 | } |
||
| 391 | |||
| 392 | public function getResourceFormat(): ?ResourceFormat |
||
| 393 | { |
||
| 394 | return $this->resourceFormat; |
||
| 395 | } |
||
| 396 | |||
| 397 | public function setResourceFormat(?ResourceFormat $resourceFormat): self |
||
| 398 | { |
||
| 399 | $this->resourceFormat = $resourceFormat; |
||
| 400 | |||
| 401 | return $this; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return Collection<int, ResourceLink> |
||
| 406 | */ |
||
| 407 | public function getResourceLinks(): Collection |
||
| 408 | { |
||
| 409 | return $this->resourceLinks; |
||
| 410 | } |
||
| 411 | |||
| 412 | public function getResourceLinkByTypeGroup( |
||
| 413 | int $resourceTypeGroup, |
||
| 414 | ?Course $course = null, |
||
| 415 | ?Session $session = null, |
||
| 416 | ?Usergroup $usergroup = null, |
||
| 417 | ?CGroup $group = null, |
||
| 418 | ?User $user = null, |
||
| 419 | ): ?ResourceLink { |
||
| 420 | $criteria = Criteria::create(); |
||
| 421 | $criteria->where( |
||
| 422 | Criteria::expr()->eq('resourceTypeGroup', $resourceTypeGroup) |
||
| 423 | ); |
||
| 424 | |||
| 425 | if ($course) { |
||
| 426 | $criteria->andWhere( |
||
| 427 | Criteria::expr()->eq('course', $course) |
||
| 428 | ); |
||
| 429 | } |
||
| 430 | |||
| 431 | if ($session) { |
||
| 432 | $criteria->andWhere( |
||
| 433 | Criteria::expr()->eq('session', $session) |
||
| 434 | ); |
||
| 435 | } |
||
| 436 | |||
| 437 | if ($usergroup) { |
||
| 438 | $criteria->andWhere( |
||
| 439 | Criteria::expr()->eq('userGroup', $usergroup) |
||
| 440 | ); |
||
| 441 | } |
||
| 442 | |||
| 443 | if ($group) { |
||
| 444 | $criteria->andWhere( |
||
| 445 | Criteria::expr()->eq('group', $group) |
||
| 446 | ); |
||
| 447 | } |
||
| 448 | |||
| 449 | if ($user) { |
||
| 450 | $criteria->andWhere( |
||
| 451 | Criteria::expr()->eq('user', $user) |
||
| 452 | ); |
||
| 453 | } |
||
| 454 | |||
| 455 | $first = $this |
||
| 456 | ->resourceLinks |
||
| 457 | ->matching($criteria) |
||
| 458 | ->first(); |
||
| 459 | |||
| 460 | return $first ?: null; |
||
| 461 | } |
||
| 462 | |||
| 463 | public function setResourceLinks(Collection $resourceLinks): self |
||
| 464 | { |
||
| 465 | $this->resourceLinks = $resourceLinks; |
||
| 466 | |||
| 467 | return $this; |
||
| 468 | } |
||
| 469 | |||
| 470 | public function addResourceLink(ResourceLink $link): self |
||
| 471 | { |
||
| 472 | $link |
||
| 473 | ->setResourceNode($this) |
||
| 474 | ->setResourceTypeGroup($link->getResourceNode()->getResourceType()->getId()) |
||
| 475 | ; |
||
| 476 | $this->resourceLinks->add($link); |
||
| 477 | |||
| 478 | return $this; |
||
| 479 | } |
||
| 480 | |||
| 481 | public function hasEditableTextContent(): bool |
||
| 482 | { |
||
| 483 | if ($this->hasResourceFile()) { |
||
| 484 | $mimeType = $this->getResourceFile()->getMimeType(); |
||
| 485 | |||
| 486 | if (str_contains($mimeType, 'text')) { |
||
| 487 | return true; |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | return false; |
||
| 492 | } |
||
| 493 | |||
| 494 | public function hasResourceFile(): bool |
||
| 495 | { |
||
| 496 | return null !== $this->resourceFile; |
||
| 497 | } |
||
| 498 | |||
| 499 | public function getResourceFile(): ?ResourceFile |
||
| 500 | { |
||
| 501 | return $this->resourceFile; |
||
| 502 | } |
||
| 503 | |||
| 504 | public function setResourceFile(?ResourceFile $resourceFile = null): self |
||
| 505 | { |
||
| 506 | $this->resourceFile = $resourceFile; |
||
| 507 | |||
| 508 | $resourceFile?->setResourceNode($this); |
||
| 509 | |||
| 510 | return $this; |
||
| 511 | } |
||
| 512 | |||
| 513 | public function getIcon(): string |
||
| 514 | { |
||
| 515 | $class = 'fa fa-folder'; |
||
| 516 | if ($this->hasResourceFile()) { |
||
| 517 | $class = 'far fa-file'; |
||
| 518 | if ($this->isResourceFileAnImage()) { |
||
| 519 | $class = 'far fa-file-image'; |
||
| 520 | } |
||
| 521 | if ($this->isResourceFileAVideo()) { |
||
| 522 | $class = 'far fa-file-video'; |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | return '<i class="'.$class.'"></i>'; |
||
| 527 | } |
||
| 528 | |||
| 529 | public function isResourceFileAnImage(): bool |
||
| 530 | { |
||
| 531 | if ($this->hasResourceFile()) { |
||
| 532 | $mimeType = $this->getResourceFile()->getMimeType(); |
||
| 533 | if (str_contains($mimeType, 'image')) { |
||
| 534 | return true; |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | return false; |
||
| 539 | } |
||
| 540 | |||
| 541 | public function isResourceFileAVideo(): bool |
||
| 542 | { |
||
| 543 | if ($this->hasResourceFile()) { |
||
| 544 | $mimeType = $this->getResourceFile()->getMimeType(); |
||
| 545 | if (str_contains($mimeType, 'video')) { |
||
| 546 | return true; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | return false; |
||
| 551 | } |
||
| 552 | |||
| 553 | public function getThumbnail(RouterInterface $router): string |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Returns the resource id. |
||
| 581 | */ |
||
| 582 | public function getId(): ?int |
||
| 583 | { |
||
| 584 | return $this->id; |
||
| 585 | } |
||
| 586 | |||
| 587 | public function getResourceType(): ResourceType |
||
| 588 | { |
||
| 589 | return $this->resourceType; |
||
| 590 | } |
||
| 591 | |||
| 592 | public function setResourceType(ResourceType $resourceType): self |
||
| 597 | } |
||
| 598 | |||
| 599 | public function getContent(): ?string |
||
| 600 | { |
||
| 601 | return $this->content; |
||
| 602 | } |
||
| 603 | |||
| 604 | public function setContent(string $content): self |
||
| 605 | { |
||
| 606 | $this->content = $content; |
||
| 607 | |||
| 608 | return $this; |
||
| 609 | } |
||
| 610 | |||
| 611 | public function getShortCut(): ?CShortcut |
||
| 612 | { |
||
| 613 | return $this->shortCut; |
||
| 614 | } |
||
| 615 | |||
| 616 | public function setShortCut(?CShortcut $shortCut): self |
||
| 617 | { |
||
| 618 | $this->shortCut = $shortCut; |
||
| 619 | |||
| 620 | return $this; |
||
| 621 | } |
||
| 622 | |||
| 623 | public function isPublic(): bool |
||
| 626 | } |
||
| 627 | |||
| 628 | public function setPublic(bool $public): self |
||
| 629 | { |
||
| 630 | $this->public = $public; |
||
| 631 | |||
| 632 | return $this; |
||
| 633 | } |
||
| 634 | } |
||
| 635 |