Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Organization 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Organization, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Organization extends BaseEntity implements OrganizationInterface, DraftableEntityInterface |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Event name of post construct event. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const postConstruct = 'postRepositoryConstruct'; |
||
|
|
|||
| 43 | |||
| 44 | /** |
||
| 45 | * externalId. Allows external applications to reference their primary key. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | * @ODM\String |
||
| 49 | * @ODM\Index |
||
| 50 | */ |
||
| 51 | protected $externalId; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The actual name of the organization. |
||
| 55 | * |
||
| 56 | * @var \Organizations\Entity\OrganizationName |
||
| 57 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", simple=true, cascade="persist") |
||
| 58 | */ |
||
| 59 | protected $organizationName; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Assigned permissions. |
||
| 63 | * |
||
| 64 | * @var PermissionsInterface |
||
| 65 | * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions") |
||
| 66 | */ |
||
| 67 | protected $permissions; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * primary logo of an organization |
||
| 71 | * |
||
| 72 | * @var \Organizations\Entity\OrganizationImage |
||
| 73 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", simple=true, nullable="true", cascade={"all"}) |
||
| 74 | */ |
||
| 75 | protected $image; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Flag indicating draft state of this job. |
||
| 79 | * |
||
| 80 | * @var bool |
||
| 81 | * @ODM\Boolean |
||
| 82 | */ |
||
| 83 | protected $isDraft = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Organization contact data. |
||
| 87 | * |
||
| 88 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact") */ |
||
| 89 | protected $contact; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The organizations' description. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | * @ODM\String |
||
| 96 | */ |
||
| 97 | protected $description; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The parent of this organization. |
||
| 101 | * |
||
| 102 | * @see setParent() |
||
| 103 | * @var OrganizationInterface | null |
||
| 104 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", simple=true, nullable=true) |
||
| 105 | * @since 0.18 |
||
| 106 | */ |
||
| 107 | protected $parent; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The hiring organizations of this organization. |
||
| 111 | * |
||
| 112 | * @var Collection |
||
| 113 | * @ODM\ReferenceMany( |
||
| 114 | * targetDocument="Organizations\Entity\Organization", |
||
| 115 | * repositoryMethod="getHiringOrganizationsCursor" |
||
| 116 | * ) |
||
| 117 | * @since 0.18 |
||
| 118 | */ |
||
| 119 | protected $hiringOrganizations; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The associated employees (users) |
||
| 123 | * |
||
| 124 | * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee") |
||
| 125 | * @var Collection |
||
| 126 | */ |
||
| 127 | protected $employees; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Jobs of this organization. |
||
| 131 | * |
||
| 132 | * @var Collection |
||
| 133 | * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", simple=true, mappedBy="organization") |
||
| 134 | * @since 0.18 |
||
| 135 | */ |
||
| 136 | protected $jobs; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * the owner of a Organization |
||
| 140 | * |
||
| 141 | * @var UserInterface $user |
||
| 142 | * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true) |
||
| 143 | * @ODM\Index |
||
| 144 | */ |
||
| 145 | protected $user; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Default values of an organizations job template |
||
| 149 | * |
||
| 150 | * @var TemplateInterface; |
||
| 151 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\Template") |
||
| 152 | */ |
||
| 153 | protected $template; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the parent of an organization |
||
| 157 | * |
||
| 158 | * @param OrganizationInterface $parent |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function setParent(OrganizationInterface $parent) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the parent of an organization |
||
| 171 | * |
||
| 172 | * @return null|OrganizationInterface |
||
| 173 | */ |
||
| 174 | public function getParent() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Gets linked organizations |
||
| 181 | * |
||
| 182 | * @return Collection |
||
| 183 | */ |
||
| 184 | public function getHiringOrganizations() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function isHiringOrganization() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Sets the external id. |
||
| 199 | * |
||
| 200 | * @param $externalId |
||
| 201 | * |
||
| 202 | * @return self |
||
| 203 | */ |
||
| 204 | public function setExternalId($externalId) |
||
| 205 | { |
||
| 206 | $this->externalId = $externalId; |
||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets the internal id. |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getExternalId() |
||
| 216 | { |
||
| 217 | return $this->externalId; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @todo review this |
||
| 222 | * @param HydratorInterface $hydrator |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function setHydrator(HydratorInterface $hydrator) |
||
| 227 | { |
||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * * @todo review this |
||
| 233 | * @return EntityHydrator |
||
| 234 | */ |
||
| 235 | public function getHydrator() |
||
| 236 | { |
||
| 237 | return new EntityHydrator(); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Sets the name of an organization |
||
| 242 | * |
||
| 243 | * @param OrganizationName $organizationName |
||
| 244 | * |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | public function setOrganizationName(OrganizationName $organizationName) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Gets the organization name entity of an organisation |
||
| 259 | * |
||
| 260 | * @return OrganizationName |
||
| 261 | */ |
||
| 262 | public function getOrganizationName() |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets the organization name |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getName() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @deprecated |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getSearchableProperties() |
||
| 286 | { |
||
| 287 | return array(); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @deprecated |
||
| 292 | * @param array $keywords |
||
| 293 | */ |
||
| 294 | public function setKeywords(array $keywords) |
||
| 295 | { |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @deprecated |
||
| 300 | */ |
||
| 301 | public function clearKeywords() |
||
| 302 | { |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @deprecated |
||
| 307 | */ |
||
| 308 | public function getKeywords() |
||
| 309 | { |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Gets the Permissions of an organization |
||
| 314 | * |
||
| 315 | * @return PermissionsInterface |
||
| 316 | */ |
||
| 317 | public function getPermissions() |
||
| 318 | { |
||
| 319 | if (!$this->permissions) { |
||
| 320 | $permissions = new Permissions(); |
||
| 321 | if ($this->user) { |
||
| 322 | $permissions->grant($this->user, Permissions::PERMISSION_ALL); |
||
| 323 | } |
||
| 324 | $this->setPermissions($permissions); |
||
| 325 | } |
||
| 326 | return $this->permissions; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Sets the Permissions of an Organization |
||
| 331 | * |
||
| 332 | * @param PermissionsInterface $permissions |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | View Code Duplication | public function setPermissions(PermissionsInterface $permissions) |
|
| 337 | { |
||
| 338 | // Assure the user has always all rights. |
||
| 339 | if ($this->user) { |
||
| 340 | $permissions->grant($this->user, Permissions::PERMISSION_ALL); |
||
| 341 | } |
||
| 342 | $this->permissions = $permissions; |
||
| 343 | return $this; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Gets the Permissions Resource ID |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function getPermissionsResourceId() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param null $type |
||
| 358 | * |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | public function getPermissionsUserIds($type = null) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Sets the logo of an organization |
||
| 402 | * |
||
| 403 | * @param OrganizationImage $image |
||
| 404 | * |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | public function setImage(OrganizationImage $image = null) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Gets the Logo of an organization |
||
| 415 | * |
||
| 416 | * @return OrganizationImage |
||
| 417 | */ |
||
| 418 | public function getImage() |
||
| 419 | { |
||
| 420 | return $this->image; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Sets the Contact Data of an organization |
||
| 425 | * |
||
| 426 | * @param EntityInterface $contact |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function setContact(EntityInterface $contact = null) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Gets the contact Data of an organization |
||
| 441 | * |
||
| 442 | * @return OrganizationContact |
||
| 443 | */ |
||
| 444 | public function getContact() |
||
| 445 | { |
||
| 446 | if (!$this->contact instanceof OrganizationContact) { |
||
| 447 | $this->contact = new OrganizationContact(); |
||
| 448 | } |
||
| 449 | return $this->contact; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Gets the Draft flag |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | public function isDraft() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Sets the draft flag |
||
| 464 | * |
||
| 465 | * @param bool $flag |
||
| 466 | * |
||
| 467 | * @return $this |
||
| 468 | */ |
||
| 469 | public function setIsDraft($flag) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Gets the default description of an organization. |
||
| 477 | * |
||
| 478 | * This description is used as the default of the company_description |
||
| 479 | * used in a job template |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getDescription() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Set the default description af an organization |
||
| 490 | * |
||
| 491 | * @param string $description |
||
| 492 | * |
||
| 493 | * @return $this |
||
| 494 | */ |
||
| 495 | public function setDescription($description) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Sets the the list of employees |
||
| 504 | * |
||
| 505 | * @param Collection $employees |
||
| 506 | * |
||
| 507 | * @return $this |
||
| 508 | */ |
||
| 509 | public function setEmployees(Collection $employees) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Gets the list of employees |
||
| 521 | * |
||
| 522 | * @return ArrayCollection|Collection |
||
| 523 | */ |
||
| 524 | public function getEmployees() |
||
| 525 | { |
||
| 526 | if ($this->isHiringOrganization()) { |
||
| 527 | // Always return empty list, as we never have employees in this case. |
||
| 528 | return new ArrayCollection(); |
||
| 529 | } |
||
| 530 | |||
| 537 | |||
| 538 | /** |
||
| 539 | * Gets an employee by User or ID. |
||
| 540 | * |
||
| 541 | * @param UserInterface|string $userOrId |
||
| 542 | * |
||
| 543 | * @return mixed|null |
||
| 544 | */ |
||
| 545 | public function getEmployee($userOrId) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Gets a list of Employees by a user role |
||
| 561 | * |
||
| 562 | * @param string $role |
||
| 563 | * @return ArrayCollection |
||
| 564 | */ |
||
| 565 | public function getEmployeesByRole($role){ |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Checks, if a User is the owner of an organization |
||
| 579 | * |
||
| 580 | * @param UserInterface $user |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function isOwner(UserInterface $user) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Returns true, if a User is an employee of the organization |
||
| 591 | * |
||
| 592 | * @param UserInterface $user |
||
| 593 | * |
||
| 594 | * @return bool |
||
| 595 | */ |
||
| 596 | public function isEmployee(UserInterface $user) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Returns true, if the user belongs to the organization. |
||
| 603 | * |
||
| 604 | * @param UserInterface $user |
||
| 605 | * |
||
| 606 | * @return bool |
||
| 607 | */ |
||
| 608 | public function isAssociated(UserInterface $user) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Updates the organizationsPermissions to allow all employees to view this organization. |
||
| 615 | * |
||
| 616 | * In case of a HiringOrganization Permissions are granted to all employees of the parent |
||
| 617 | * organization. |
||
| 618 | * |
||
| 619 | * @ODM\PreUpdate |
||
| 620 | * @ODM\PrePersist |
||
| 621 | * @since 0.18 |
||
| 622 | */ |
||
| 623 | public function updatePermissions() |
||
| 645 | |||
| 646 | View Code Duplication | public function setUser(UserInterface $user) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Gets the owner of the organization |
||
| 658 | * |
||
| 659 | * @return UserInterface |
||
| 660 | */ |
||
| 661 | public function getUser() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Gets the Jobs of an organization |
||
| 668 | * |
||
| 669 | * @return Collection |
||
| 670 | */ |
||
| 671 | public function getJobs() |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Gets default values of an organizations job template |
||
| 678 | * |
||
| 679 | * @return TemplateInterface |
||
| 680 | */ |
||
| 681 | public function getTemplate() |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Sets default values of an organizations job template |
||
| 691 | * |
||
| 692 | * @return self |
||
| 693 | */ |
||
| 694 | public function setTemplate(TemplateInterface $template) |
||
| 698 | } |