| Total Complexity | 51 |
| Total Lines | 535 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AccessUrl 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 AccessUrl, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class AccessUrl extends AbstractResource implements ResourceInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @ORM\Column(name="id", type="integer") |
||
| 34 | * @ORM\Id |
||
| 35 | * @ORM\GeneratedValue() |
||
| 36 | * |
||
| 37 | * @Groups({"access_url:read", "access_url:write"}) |
||
| 38 | */ |
||
| 39 | protected int $id; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var AccessUrlRelCourse[]|Collection<int, AccessUrlRelCourse> |
||
| 43 | * |
||
| 44 | * @ORM\OneToMany(targetEntity="AccessUrlRelCourse", mappedBy="url", cascade={"persist"}, orphanRemoval=true) |
||
| 45 | */ |
||
| 46 | protected Collection $courses; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var AccessUrlRelSession[]|Collection<int, AccessUrlRelSession> |
||
| 50 | * |
||
| 51 | * @ORM\OneToMany(targetEntity="AccessUrlRelSession", mappedBy="url", cascade={"persist"}, orphanRemoval=true) |
||
| 52 | */ |
||
| 53 | protected Collection $sessions; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ORM\OneToMany(targetEntity="AccessUrlRelUser", mappedBy="url", cascade={"persist"}, orphanRemoval=true) |
||
| 57 | * |
||
| 58 | * @var AccessUrlRelUser[]|Collection<int, AccessUrlRelUser> |
||
| 59 | */ |
||
| 60 | protected Collection $users; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @ORM\OneToMany(targetEntity="SettingsCurrent", mappedBy="url", cascade={"persist"}, orphanRemoval=true) |
||
| 64 | * |
||
| 65 | * @var Collection<int, SettingsCurrent>|SettingsCurrent[] |
||
| 66 | */ |
||
| 67 | protected Collection $settings; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @ORM\OneToMany(targetEntity="SessionCategory", mappedBy="url", cascade={"persist"}, orphanRemoval=true) |
||
| 71 | * |
||
| 72 | * @var Collection<int, SessionCategory>|SessionCategory[] |
||
| 73 | */ |
||
| 74 | protected Collection $sessionCategories; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @ORM\OneToMany(targetEntity="AccessUrlRelCourseCategory", mappedBy="url", cascade={"persist"}, orphanRemoval=true) |
||
| 78 | * |
||
| 79 | * @var AccessUrlRelCourseCategory[]|Collection<int, AccessUrlRelCourseCategory> |
||
| 80 | */ |
||
| 81 | protected Collection $courseCategory; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @Gedmo\TreeParent |
||
| 85 | * |
||
| 86 | * @ORM\ManyToOne( |
||
| 87 | * targetEntity="Chamilo\CoreBundle\Entity\AccessUrl", |
||
| 88 | * inversedBy="children" |
||
| 89 | * ) |
||
| 90 | * @ORM\JoinColumns({ |
||
| 91 | * @ORM\JoinColumn(onDelete="CASCADE") |
||
| 92 | * }) |
||
| 93 | */ |
||
| 94 | protected ?AccessUrl $parent = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var AccessUrl[]|Collection<int, AccessUrl> |
||
| 98 | * |
||
| 99 | * @ORM\OneToMany( |
||
| 100 | * targetEntity="Chamilo\CoreBundle\Entity\AccessUrl", |
||
| 101 | * mappedBy="parent" |
||
| 102 | * ) |
||
| 103 | * @ORM\OrderBy({"id" = "ASC"}) |
||
| 104 | */ |
||
| 105 | protected Collection $children; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @Gedmo\TreeLeft |
||
| 109 | * @ORM\Column(name="lft", type="integer") |
||
| 110 | */ |
||
| 111 | protected int $lft; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @Gedmo\TreeLevel |
||
| 115 | * @ORM\Column(name="lvl", type="integer") |
||
| 116 | */ |
||
| 117 | protected int $lvl; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @Gedmo\TreeRight |
||
| 121 | * @ORM\Column(name="rgt", type="integer") |
||
| 122 | */ |
||
| 123 | protected int $rgt; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @Gedmo\TreeRoot |
||
| 127 | * @ORM\ManyToOne(targetEntity="AccessUrl") |
||
| 128 | * @ORM\JoinColumn(name="tree_root", onDelete="CASCADE") |
||
| 129 | */ |
||
| 130 | protected ?AccessUrl $root = null; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @Assert\NotBlank() |
||
| 134 | * @Groups({"access_url:read", "access_url:write"}) |
||
| 135 | * |
||
| 136 | * @ORM\Column(name="url", type="string", length=255) |
||
| 137 | */ |
||
| 138 | protected string $url; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @ORM\Column(name="description", type="text") |
||
| 142 | */ |
||
| 143 | protected ?string $description = null; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @ORM\Column(name="active", type="integer") |
||
| 147 | */ |
||
| 148 | protected int $active; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @ORM\Column(name="created_by", type="integer") |
||
| 152 | */ |
||
| 153 | protected int $createdBy; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @ORM\Column(name="tms", type="datetime", nullable=true) |
||
| 157 | */ |
||
| 158 | protected ?DateTime $tms; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @ORM\Column(name="url_type", type="boolean", nullable=true) |
||
| 162 | */ |
||
| 163 | protected ?bool $urlType = null; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @ORM\Column(name="limit_courses", type="integer", nullable=true) |
||
| 167 | */ |
||
| 168 | protected ?int $limitCourses = null; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @ORM\Column(name="limit_active_courses", type="integer", nullable=true) |
||
| 172 | */ |
||
| 173 | protected ?int $limitActiveCourses = null; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @ORM\Column(name="limit_sessions", type="integer", nullable=true) |
||
| 177 | */ |
||
| 178 | protected ?int $limitSessions = null; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @ORM\Column(name="limit_users", type="integer", nullable=true) |
||
| 182 | */ |
||
| 183 | protected ?int $limitUsers = null; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @ORM\Column(name="limit_teachers", type="integer", nullable=true) |
||
| 187 | */ |
||
| 188 | protected ?int $limitTeachers = null; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @ORM\Column(name="limit_disk_space", type="integer", nullable=true) |
||
| 192 | */ |
||
| 193 | protected ?int $limitDiskSpace = null; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @ORM\Column(name="email", type="string", length=255, nullable=true) |
||
| 197 | */ |
||
| 198 | protected ?string $email = null; |
||
| 199 | |||
| 200 | public function __construct() |
||
| 201 | { |
||
| 202 | $this->tms = new DateTime(); |
||
| 203 | $this->createdBy = 1; |
||
| 204 | $this->courses = new ArrayCollection(); |
||
| 205 | $this->sessions = new ArrayCollection(); |
||
| 206 | $this->users = new ArrayCollection(); |
||
| 207 | $this->settings = new ArrayCollection(); |
||
| 208 | $this->sessionCategories = new ArrayCollection(); |
||
| 209 | $this->courseCategory = new ArrayCollection(); |
||
| 210 | $this->children = new ArrayCollection(); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function __toString(): string |
||
| 214 | { |
||
| 215 | return $this->getUrl(); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get id. |
||
| 220 | * |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | public function getId() |
||
| 224 | { |
||
| 225 | return $this->id; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function setUrl(string $url): self |
||
| 229 | { |
||
| 230 | $this->url = $url; |
||
| 231 | |||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function getUrl(): string |
||
| 236 | { |
||
| 237 | return $this->url; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function setDescription(string $description): self |
||
| 241 | { |
||
| 242 | $this->description = $description; |
||
| 243 | |||
| 244 | return $this; |
||
| 245 | } |
||
| 246 | |||
| 247 | public function getDescription(): ?string |
||
| 248 | { |
||
| 249 | return $this->description; |
||
| 250 | } |
||
| 251 | |||
| 252 | public function setActive(int $active): self |
||
| 253 | { |
||
| 254 | $this->active = $active; |
||
| 255 | |||
| 256 | return $this; |
||
| 257 | } |
||
| 258 | |||
| 259 | public function getActive() |
||
| 260 | { |
||
| 261 | return $this->active; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function setCreatedBy(int $createdBy): self |
||
| 265 | { |
||
| 266 | $this->createdBy = $createdBy; |
||
| 267 | |||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get createdBy. |
||
| 273 | * |
||
| 274 | * @return int |
||
| 275 | */ |
||
| 276 | public function getCreatedBy() |
||
| 277 | { |
||
| 278 | return $this->createdBy; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function setTms(DateTime $tms): self |
||
| 282 | { |
||
| 283 | $this->tms = $tms; |
||
| 284 | |||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get tms. |
||
| 290 | * |
||
| 291 | * @return DateTime |
||
| 292 | */ |
||
| 293 | public function getTms() |
||
| 294 | { |
||
| 295 | return $this->tms; |
||
| 296 | } |
||
| 297 | |||
| 298 | public function setUrlType(bool $urlType): self |
||
| 299 | { |
||
| 300 | $this->urlType = $urlType; |
||
| 301 | |||
| 302 | return $this; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get urlType. |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function getUrlType() |
||
| 311 | { |
||
| 312 | return $this->urlType; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return int |
||
| 317 | */ |
||
| 318 | public function getLimitActiveCourses() |
||
| 321 | } |
||
| 322 | |||
| 323 | public function setLimitActiveCourses(int $limitActiveCourses): self |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | public function getLimitSessions() |
||
| 336 | } |
||
| 337 | |||
| 338 | public function setLimitSessions(int $limitSessions): self |
||
| 339 | { |
||
| 340 | $this->limitSessions = $limitSessions; |
||
| 341 | |||
| 342 | return $this; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return int |
||
| 347 | */ |
||
| 348 | public function getLimitUsers() |
||
| 349 | { |
||
| 350 | return $this->limitUsers; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function setLimitUsers(int $limitUsers): self |
||
| 354 | { |
||
| 355 | $this->limitUsers = $limitUsers; |
||
| 356 | |||
| 357 | return $this; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return int |
||
| 362 | */ |
||
| 363 | public function getLimitTeachers() |
||
| 364 | { |
||
| 365 | return $this->limitTeachers; |
||
| 366 | } |
||
| 367 | |||
| 368 | public function setLimitTeachers(int $limitTeachers): self |
||
| 369 | { |
||
| 370 | $this->limitTeachers = $limitTeachers; |
||
| 371 | |||
| 372 | return $this; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return int |
||
| 377 | */ |
||
| 378 | public function getLimitDiskSpace() |
||
| 381 | } |
||
| 382 | |||
| 383 | public function setLimitDiskSpace(int $limitDiskSpace): self |
||
| 384 | { |
||
| 385 | $this->limitDiskSpace = $limitDiskSpace; |
||
| 386 | |||
| 387 | return $this; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function getEmail() |
||
| 394 | { |
||
| 395 | return $this->email; |
||
| 396 | } |
||
| 397 | |||
| 398 | public function setEmail(string $email): self |
||
| 399 | { |
||
| 400 | $this->email = $email; |
||
| 401 | |||
| 402 | return $this; |
||
| 403 | } |
||
| 404 | |||
| 405 | public function getSettings() |
||
| 406 | { |
||
| 407 | return $this->settings; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param Collection<int, SettingsCurrent>|SettingsCurrent[] $settings |
||
| 412 | */ |
||
| 413 | public function setSettings(Collection $settings): self |
||
| 414 | { |
||
| 415 | $this->settings = $settings; |
||
| 416 | |||
| 417 | return $this; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return int |
||
| 422 | */ |
||
| 423 | public function getLimitCourses() |
||
| 424 | { |
||
| 425 | return $this->limitCourses; |
||
| 426 | } |
||
| 427 | |||
| 428 | public function setLimitCourses(int $limitCourses): self |
||
| 429 | { |
||
| 430 | $this->limitCourses = $limitCourses; |
||
| 431 | |||
| 432 | return $this; |
||
| 433 | } |
||
| 434 | |||
| 435 | public function getCourses() |
||
| 436 | { |
||
| 437 | return $this->courses; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param AccessUrlRelCourse[]|Collection<int, AccessUrlRelCourse> $courses |
||
| 442 | */ |
||
| 443 | public function setCourses(Collection $courses): self |
||
| 444 | { |
||
| 445 | $this->courses = $courses; |
||
| 446 | |||
| 447 | return $this; |
||
| 448 | } |
||
| 449 | |||
| 450 | public function getSessionCategories() |
||
| 451 | { |
||
| 452 | return $this->sessionCategories; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param Collection<int, SessionCategory>|SessionCategory[] $sessionCategories |
||
| 457 | */ |
||
| 458 | public function setSessionCategories(Collection $sessionCategories): self |
||
| 459 | { |
||
| 460 | $this->sessionCategories = $sessionCategories; |
||
| 461 | |||
| 462 | return $this; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @return AccessUrlRelSession[]|Collection |
||
| 467 | */ |
||
| 468 | public function getSessions() |
||
| 469 | { |
||
| 470 | return $this->sessions; |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @return AccessUrl[]|Collection |
||
| 475 | */ |
||
| 476 | public function getChildren() |
||
| 477 | { |
||
| 478 | return $this->children; |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return AccessUrlRelUser[]|Collection |
||
| 483 | */ |
||
| 484 | public function getUsers() |
||
| 485 | { |
||
| 486 | return $this->users; |
||
| 487 | } |
||
| 488 | |||
| 489 | public function addUser(User $user): self |
||
| 490 | { |
||
| 491 | if (!$this->hasUser($user)) { |
||
| 492 | $accessUrlRelUser = (new AccessUrlRelUser()) |
||
| 493 | ->setUser($user) |
||
| 494 | ->setUrl($this) |
||
| 495 | ; |
||
| 496 | $this->users->add($accessUrlRelUser); |
||
| 497 | } |
||
| 498 | |||
| 499 | return $this; |
||
| 500 | } |
||
| 501 | |||
| 502 | public function hasUser(User $user): bool |
||
| 503 | { |
||
| 504 | if (0 !== $this->users->count()) { |
||
| 505 | $criteria = Criteria::create()->where( |
||
| 506 | Criteria::expr()->eq('user', $user) |
||
| 507 | ); |
||
| 508 | $relation = $this->users->matching($criteria); |
||
| 509 | |||
| 510 | return $relation->count() > 0; |
||
| 511 | } |
||
| 512 | |||
| 513 | return false; |
||
| 514 | } |
||
| 515 | |||
| 516 | public function getParent(): ?self |
||
| 517 | { |
||
| 518 | return $this->parent; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @return AccessUrlRelCourseCategory[]|Collection |
||
| 523 | */ |
||
| 524 | public function getCourseCategory() |
||
| 525 | { |
||
| 526 | return $this->courseCategory; |
||
| 527 | } |
||
| 528 | |||
| 529 | public function getLft(): int |
||
| 530 | { |
||
| 531 | return $this->lft; |
||
| 532 | } |
||
| 533 | |||
| 534 | public function getLvl(): int |
||
| 535 | { |
||
| 536 | return $this->lvl; |
||
| 537 | } |
||
| 538 | |||
| 539 | public function getRgt(): int |
||
| 540 | { |
||
| 541 | return $this->rgt; |
||
| 542 | } |
||
| 543 | |||
| 544 | public function getRoot(): ?self |
||
| 545 | { |
||
| 546 | return $this->root; |
||
| 547 | } |
||
| 548 | |||
| 549 | public function getResourceIdentifier(): int |
||
| 552 | } |
||
| 553 | |||
| 554 | public function getResourceName(): string |
||
| 555 | { |
||
| 556 | $url = $this->getUrl(); |
||
| 557 | $url = parse_url($url); |
||
| 558 | |||
| 559 | return $url['host']; |
||
| 560 | } |
||
| 561 | |||
| 562 | public function setResourceName(string $name): self |
||
| 563 | { |
||
| 564 | return $this->setUrl($name); |
||
| 565 | } |
||
| 566 | } |
||
| 567 |