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 | * Event name of post construct event. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const postConstruct = 'postRepositoryConstruct'; |
||
|
|
|||
| 42 | |||
| 43 | /** |
||
| 44 | * externalId. Allows external applications to reference their primary key. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | * @ODM\Field(type="string") |
||
| 48 | * @ODM\Index |
||
| 49 | */ |
||
| 50 | protected $externalId; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The actual name of the organization. |
||
| 54 | * |
||
| 55 | * @var \Organizations\Entity\OrganizationName |
||
| 56 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", simple=true, cascade="persist") |
||
| 57 | */ |
||
| 58 | protected $organizationName; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Assigned permissions. |
||
| 62 | * |
||
| 63 | * @var PermissionsInterface |
||
| 64 | * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions") |
||
| 65 | */ |
||
| 66 | protected $permissions; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * primary logo of an organization |
||
| 70 | * |
||
| 71 | * @var \Organizations\Entity\OrganizationImage |
||
| 72 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", simple=true, nullable="true", cascade={"all"}) |
||
| 73 | */ |
||
| 74 | protected $image; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Flag indicating draft state of this job. |
||
| 78 | * |
||
| 79 | * @var bool |
||
| 80 | * @ODM\Boolean |
||
| 81 | */ |
||
| 82 | protected $isDraft = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Organization contact data. |
||
| 86 | * |
||
| 87 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact") */ |
||
| 88 | protected $contact; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The organizations' description. |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | * @ODM\Field(type="string") |
||
| 95 | */ |
||
| 96 | protected $description; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The parent of this organization. |
||
| 100 | * |
||
| 101 | * @see setParent() |
||
| 102 | * @var OrganizationInterface | null |
||
| 103 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", simple=true, nullable=true) |
||
| 104 | * @since 0.18 |
||
| 105 | */ |
||
| 106 | protected $parent; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The hiring organizations of this organization. |
||
| 110 | * |
||
| 111 | * @var Collection |
||
| 112 | * @ODM\ReferenceMany( |
||
| 113 | * targetDocument="Organizations\Entity\Organization", |
||
| 114 | * repositoryMethod="getHiringOrganizationsCursor" |
||
| 115 | * ) |
||
| 116 | * @since 0.18 |
||
| 117 | */ |
||
| 118 | protected $hiringOrganizations; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The associated employees (users) |
||
| 122 | * |
||
| 123 | * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee") |
||
| 124 | * @var Collection |
||
| 125 | */ |
||
| 126 | protected $employees; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Jobs of this organization. |
||
| 130 | * |
||
| 131 | * @var Collection |
||
| 132 | * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", simple=true, mappedBy="organization") |
||
| 133 | * @since 0.18 |
||
| 134 | */ |
||
| 135 | protected $jobs; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * the owner of a Organization |
||
| 139 | * |
||
| 140 | * @var UserInterface $user |
||
| 141 | * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true) |
||
| 142 | * @ODM\Index |
||
| 143 | */ |
||
| 144 | protected $user; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Default values of an organizations job template |
||
| 148 | * |
||
| 149 | * @var TemplateInterface; |
||
| 150 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\Template") |
||
| 151 | */ |
||
| 152 | protected $template; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Default values Workflow |
||
| 156 | * |
||
| 157 | * @var WorkflowSettingsInterface $workflowSettings; |
||
| 158 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\WorkflowSettings") |
||
| 159 | */ |
||
| 160 | protected $workflowSettings; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sets the parent of an organization |
||
| 164 | * |
||
| 165 | * @param OrganizationInterface $parent |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function setParent(OrganizationInterface $parent) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets the parent of an organization |
||
| 178 | * |
||
| 179 | * @return null|OrganizationInterface |
||
| 180 | */ |
||
| 181 | public function getParent() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Gets linked organizations |
||
| 188 | * |
||
| 189 | * @return Collection |
||
| 190 | */ |
||
| 191 | public function getHiringOrganizations() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isHiringOrganization() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Sets the external id. |
||
| 206 | * |
||
| 207 | * @param $externalId |
||
| 208 | * |
||
| 209 | * @return self |
||
| 210 | */ |
||
| 211 | public function setExternalId($externalId) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Gets the internal id. |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function getExternalId() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @todo review this |
||
| 229 | * @param HydratorInterface $hydrator |
||
| 230 | * |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function setHydrator(HydratorInterface $hydrator) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * * @todo review this |
||
| 240 | * @return EntityHydrator |
||
| 241 | */ |
||
| 242 | public function getHydrator() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Sets the name of an organization |
||
| 249 | * |
||
| 250 | * @param OrganizationName $organizationName |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function setOrganizationName(OrganizationName $organizationName) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Gets the organization name entity of an organisation |
||
| 266 | * |
||
| 267 | * @return OrganizationName |
||
| 268 | */ |
||
| 269 | public function getOrganizationName() |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Gets the organization name |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getName() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @deprecated |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function getSearchableProperties() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Gets the Permissions of an organization |
||
| 299 | * |
||
| 300 | * @return PermissionsInterface |
||
| 301 | */ |
||
| 302 | public function getPermissions() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Sets the Permissions of an Organization |
||
| 316 | * |
||
| 317 | * @param PermissionsInterface $permissions |
||
| 318 | * |
||
| 319 | * @return $this |
||
| 320 | */ |
||
| 321 | View Code Duplication | public function setPermissions(PermissionsInterface $permissions) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Gets the Permissions Resource ID |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function getPermissionsResourceId() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param null $type |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function getPermissionsUserIds($type = null) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Sets the logo of an organization |
||
| 387 | * |
||
| 388 | * @param OrganizationImage $image |
||
| 389 | * |
||
| 390 | * @return self |
||
| 391 | */ |
||
| 392 | public function setImage(OrganizationImage $image = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Gets the Logo of an organization |
||
| 400 | * |
||
| 401 | * @return OrganizationImage |
||
| 402 | */ |
||
| 403 | public function getImage() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Sets the Contact Data of an organization |
||
| 410 | * |
||
| 411 | * @param EntityInterface $contact |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | public function setContact(EntityInterface $contact = null) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Gets the contact Data of an organization |
||
| 426 | * |
||
| 427 | * @return OrganizationContact |
||
| 428 | */ |
||
| 429 | public function getContact() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Gets the Draft flag |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function isDraft() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Sets the draft flag |
||
| 449 | * |
||
| 450 | * @param bool $flag |
||
| 451 | * |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | public function setIsDraft($flag) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Gets the default description of an organization. |
||
| 462 | * |
||
| 463 | * This description is used as the default of the company_description |
||
| 464 | * used in a job template |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public function getDescription() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Set the default description af an organization |
||
| 475 | * |
||
| 476 | * @param string $description |
||
| 477 | * |
||
| 478 | * @return $this |
||
| 479 | */ |
||
| 480 | public function setDescription($description) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Sets the the list of employees |
||
| 489 | * |
||
| 490 | * @param Collection $employees |
||
| 491 | * |
||
| 492 | * @return $this |
||
| 493 | */ |
||
| 494 | public function setEmployees(Collection $employees) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Gets the list of employees |
||
| 506 | * |
||
| 507 | * @return ArrayCollection|Collection |
||
| 508 | */ |
||
| 509 | public function getEmployees() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Gets an employee by User or ID. |
||
| 525 | * |
||
| 526 | * @param UserInterface|string $userOrId |
||
| 527 | * |
||
| 528 | * @return mixed|null |
||
| 529 | */ |
||
| 530 | public function getEmployee($userOrId) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Gets a list of Employees by a user role |
||
| 546 | * |
||
| 547 | * @param string $role |
||
| 548 | * @return ArrayCollection |
||
| 549 | */ |
||
| 550 | public function getEmployeesByRole($role){ |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Checks, if a User is the owner of an organization |
||
| 564 | * |
||
| 565 | * @param UserInterface $user |
||
| 566 | * |
||
| 567 | * @return bool |
||
| 568 | */ |
||
| 569 | public function isOwner(UserInterface $user) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns true, if a User is an employee of the organization |
||
| 576 | * |
||
| 577 | * @param UserInterface $user |
||
| 578 | * |
||
| 579 | * @return bool |
||
| 580 | */ |
||
| 581 | public function isEmployee(UserInterface $user) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Returns true, if the user belongs to the organization. |
||
| 588 | * |
||
| 589 | * @param UserInterface $user |
||
| 590 | * |
||
| 591 | * @return bool |
||
| 592 | */ |
||
| 593 | public function isAssociated(UserInterface $user) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Updates the organizationsPermissions to allow all employees to view this organization. |
||
| 600 | * |
||
| 601 | * In case of a HiringOrganization Permissions are granted to all employees of the parent |
||
| 602 | * organization. |
||
| 603 | * |
||
| 604 | * @ODM\PreUpdate |
||
| 605 | * @ODM\PrePersist |
||
| 606 | * @since 0.18 |
||
| 607 | */ |
||
| 608 | public function updatePermissions() |
||
| 630 | |||
| 631 | View Code Duplication | public function setUser(UserInterface $user) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Gets the owner of the organization |
||
| 643 | * |
||
| 644 | * @return UserInterface |
||
| 645 | */ |
||
| 646 | public function getUser() |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Gets the Jobs of an organization |
||
| 653 | * |
||
| 654 | * @return Collection |
||
| 655 | */ |
||
| 656 | public function getJobs() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Gets default values of an organizations job template |
||
| 663 | * |
||
| 664 | * @return TemplateInterface |
||
| 665 | */ |
||
| 666 | public function getTemplate() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Sets default values of an organizations job template |
||
| 676 | * |
||
| 677 | * @return self |
||
| 678 | */ |
||
| 679 | public function setTemplate(TemplateInterface $template) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Gets Workflow Settings |
||
| 687 | * |
||
| 688 | * @return WorkflowSettings|WorkflowSettingsInterface |
||
| 689 | */ |
||
| 690 | public function getWorkflowSettings(){ |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Sets Workflow Settings |
||
| 699 | * |
||
| 700 | * @param $workflowSettings |
||
| 701 | * |
||
| 702 | * @return self |
||
| 703 | */ |
||
| 704 | public function setWorkflowSettings($workflowSettings){ |
||
| 708 | } |