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 |
||
| 38 | class Organization extends BaseEntity implements OrganizationInterface, DraftableEntityInterface |
||
|
|
|||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Event name of post construct event. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const postConstruct = 'postRepositoryConstruct'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * externalId. Allows external applications to reference their primary key. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | * @ODM\Field(type="string") |
||
| 52 | * @ODM\Index |
||
| 53 | */ |
||
| 54 | protected $externalId; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The actual name of the organization. |
||
| 58 | * |
||
| 59 | * @var \Organizations\Entity\OrganizationName |
||
| 60 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", simple=true, cascade="persist") |
||
| 61 | */ |
||
| 62 | protected $organizationName; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Only for sorting/searching purposes |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | * @ODM\Field(type="string") |
||
| 69 | */ |
||
| 70 | protected $_organizationName; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Assigned permissions. |
||
| 74 | * |
||
| 75 | * @var PermissionsInterface |
||
| 76 | * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions") |
||
| 77 | */ |
||
| 78 | protected $permissions; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * primary logo of an organization |
||
| 82 | * |
||
| 83 | * @var \Organizations\Entity\OrganizationImage |
||
| 84 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", simple=true, nullable="true", cascade={"all"}) |
||
| 85 | */ |
||
| 86 | protected $image; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Flag indicating draft state of this job. |
||
| 90 | * |
||
| 91 | * @var bool |
||
| 92 | * @ODM\Boolean |
||
| 93 | */ |
||
| 94 | protected $isDraft = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Organization contact data. |
||
| 98 | * |
||
| 99 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact") */ |
||
| 100 | protected $contact; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The organizations' description. |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | * @ODM\Field(type="string") |
||
| 107 | */ |
||
| 108 | protected $description; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The parent of this organization. |
||
| 112 | * |
||
| 113 | * @see setParent() |
||
| 114 | * @var OrganizationInterface | null |
||
| 115 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", simple=true, nullable=true) |
||
| 116 | * @since 0.18 |
||
| 117 | */ |
||
| 118 | protected $parent; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The hiring organizations of this organization. |
||
| 122 | * |
||
| 123 | * @var Collection |
||
| 124 | * @ODM\ReferenceMany( |
||
| 125 | * targetDocument="Organizations\Entity\Organization", |
||
| 126 | * repositoryMethod="getHiringOrganizationsCursor" |
||
| 127 | * ) |
||
| 128 | * @since 0.18 |
||
| 129 | */ |
||
| 130 | protected $hiringOrganizations; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * The associated employees (users) |
||
| 134 | * |
||
| 135 | * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee") |
||
| 136 | * @var Collection |
||
| 137 | */ |
||
| 138 | protected $employees; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Jobs of this organization. |
||
| 142 | * |
||
| 143 | * @var Collection |
||
| 144 | * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", simple=true, mappedBy="organization") |
||
| 145 | * @since 0.18 |
||
| 146 | */ |
||
| 147 | protected $jobs; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * the owner of a Organization |
||
| 151 | * |
||
| 152 | * @var UserInterface $user |
||
| 153 | * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true) |
||
| 154 | * @ODM\Index |
||
| 155 | */ |
||
| 156 | protected $user; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Default values of an organizations job template |
||
| 160 | * |
||
| 161 | * @var TemplateInterface; |
||
| 162 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\Template") |
||
| 163 | */ |
||
| 164 | protected $template; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Default values Workflow |
||
| 168 | * |
||
| 169 | * @var WorkflowSettingsInterface $workflowSettings; |
||
| 170 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\WorkflowSettings") |
||
| 171 | */ |
||
| 172 | protected $workflowSettings; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Sets the parent of an organization |
||
| 176 | * |
||
| 177 | * @param OrganizationInterface $parent |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function setParent(OrganizationInterface $parent) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Gets the parent of an organization |
||
| 190 | * |
||
| 191 | * @param bool $returnSelf returns itself, if this organization does not have a parent? |
||
| 192 | * |
||
| 193 | * @return null|OrganizationInterface |
||
| 194 | */ |
||
| 195 | public function getParent($returnSelf = false) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Gets linked organizations |
||
| 202 | * |
||
| 203 | * @return Collection |
||
| 204 | */ |
||
| 205 | public function getHiringOrganizations() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function isHiringOrganization() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the external id. |
||
| 220 | * |
||
| 221 | * @param $externalId |
||
| 222 | * |
||
| 223 | * @return self |
||
| 224 | */ |
||
| 225 | public function setExternalId($externalId) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Gets the internal id. |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getExternalId() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @todo review this |
||
| 243 | * @param HydratorInterface $hydrator |
||
| 244 | * |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | public function setHydrator(HydratorInterface $hydrator) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * * @todo review this |
||
| 254 | * @return EntityHydrator |
||
| 255 | */ |
||
| 256 | public function getHydrator() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Sets the name of an organization |
||
| 263 | * |
||
| 264 | * @param OrganizationName $organizationName |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function setOrganizationName(OrganizationName $organizationName) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Gets the organization name entity of an organisation |
||
| 281 | * |
||
| 282 | * @return OrganizationName |
||
| 283 | */ |
||
| 284 | public function getOrganizationName() |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Gets the organization name |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getName() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @deprecated |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public function getSearchableProperties() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Gets the Permissions of an organization |
||
| 314 | * |
||
| 315 | * @return PermissionsInterface |
||
| 316 | */ |
||
| 317 | public function getPermissions() |
||
| 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) |
|
| 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) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Sets the logo of an organization |
||
| 404 | * |
||
| 405 | * @param OrganizationImage $image |
||
| 406 | * |
||
| 407 | * @return self |
||
| 408 | */ |
||
| 409 | public function setImage(OrganizationImage $image = null) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Gets the Logo of an organization |
||
| 417 | * |
||
| 418 | * @return OrganizationImage |
||
| 419 | */ |
||
| 420 | public function getImage() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Sets the Contact Data of an organization |
||
| 427 | * |
||
| 428 | * @param EntityInterface $contact |
||
| 429 | * |
||
| 430 | * @return $this |
||
| 431 | */ |
||
| 432 | public function setContact(EntityInterface $contact = null) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Gets the contact Data of an organization |
||
| 443 | * |
||
| 444 | * @return OrganizationContact |
||
| 445 | */ |
||
| 446 | public function getContact() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Gets the Draft flag |
||
| 456 | * |
||
| 457 | * @return bool |
||
| 458 | */ |
||
| 459 | public function isDraft() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Sets the draft flag |
||
| 466 | * |
||
| 467 | * @param bool $flag |
||
| 468 | * |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function setIsDraft($flag) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Gets the default description of an organization. |
||
| 479 | * |
||
| 480 | * This description is used as the default of the company_description |
||
| 481 | * used in a job template |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | public function getDescription() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Set the default description af an organization |
||
| 492 | * |
||
| 493 | * @param string $description |
||
| 494 | * |
||
| 495 | * @return $this |
||
| 496 | */ |
||
| 497 | public function setDescription($description) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Sets the the list of employees |
||
| 506 | * |
||
| 507 | * @param Collection $employees |
||
| 508 | * |
||
| 509 | * @return $this |
||
| 510 | */ |
||
| 511 | public function setEmployees(Collection $employees) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Gets the list of employees |
||
| 523 | * |
||
| 524 | * @return ArrayCollection|Collection |
||
| 525 | */ |
||
| 526 | public function getEmployees() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Gets an employee by User or ID. |
||
| 542 | * |
||
| 543 | * @param UserInterface|string $userOrId |
||
| 544 | * |
||
| 545 | * @return mixed|null |
||
| 546 | */ |
||
| 547 | public function getEmployee($userOrId) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Gets a list of Employees by a user role |
||
| 563 | * |
||
| 564 | * @param string $role |
||
| 565 | * @return ArrayCollection |
||
| 566 | */ |
||
| 567 | public function getEmployeesByRole($role){ |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Checks, if a User is the owner of an organization |
||
| 581 | * |
||
| 582 | * @param UserInterface $user |
||
| 583 | * |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | public function isOwner(UserInterface $user) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns true, if a User is an employee of the organization |
||
| 593 | * |
||
| 594 | * @param UserInterface $user |
||
| 595 | * |
||
| 596 | * @return bool |
||
| 597 | */ |
||
| 598 | public function isEmployee(UserInterface $user) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Returns true, if the user belongs to the organization. |
||
| 605 | * |
||
| 606 | * @param UserInterface $user |
||
| 607 | * |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | public function isAssociated(UserInterface $user) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Updates the organizationsPermissions to allow all employees to view this organization. |
||
| 617 | * |
||
| 618 | * In case of a HiringOrganization Permissions are granted to all employees of the parent |
||
| 619 | * organization. |
||
| 620 | * |
||
| 621 | * @ODM\PreUpdate |
||
| 622 | * @ODM\PrePersist |
||
| 623 | * @since 0.18 |
||
| 624 | */ |
||
| 625 | public function updatePermissions() |
||
| 647 | |||
| 648 | View Code Duplication | public function setUser(UserInterface $user) |
|
| 657 | |||
| 658 | /** |
||
| 659 | * Gets the owner of the organization |
||
| 660 | * |
||
| 661 | * @return UserInterface |
||
| 662 | */ |
||
| 663 | public function getUser() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Gets the Jobs of an organization |
||
| 670 | * |
||
| 671 | * @return Collection |
||
| 672 | */ |
||
| 673 | public function getJobs() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Gets default values of an organizations job template |
||
| 680 | * |
||
| 681 | * @return TemplateInterface |
||
| 682 | */ |
||
| 683 | public function getTemplate() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Sets default values of an organizations job template |
||
| 693 | * |
||
| 694 | * @return self |
||
| 695 | */ |
||
| 696 | public function setTemplate(TemplateInterface $template) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Gets Workflow Settings |
||
| 704 | * |
||
| 705 | * @return WorkflowSettings|WorkflowSettingsInterface |
||
| 706 | */ |
||
| 707 | public function getWorkflowSettings(){ |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Sets Workflow Settings |
||
| 716 | * |
||
| 717 | * @param $workflowSettings |
||
| 718 | * |
||
| 719 | * @return self |
||
| 720 | */ |
||
| 721 | public function setWorkflowSettings($workflowSettings){ |
||
| 725 | } |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.