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 DNEnvironment 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 DNEnvironment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class DNEnvironment extends DataObject { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * If this is set to a full pathfile, it will be used as template |
||
| 40 | * file when creating a new capistrano environment config file. |
||
| 41 | * |
||
| 42 | * If not set, the default 'environment.template' from the module |
||
| 43 | * root is used |
||
| 44 | * |
||
| 45 | * @config |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private static $template_file = ''; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set this to true to allow editing of the environment files via the web admin |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private static $allow_web_editing = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private static $casting = array( |
||
| 61 | 'DeployHistory' => 'Text' |
||
| 62 | ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Allowed backends. A map of Injector identifier to human-readable label. |
||
| 66 | * |
||
| 67 | * @config |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private static $allowed_backends = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | public static $db = array( |
||
| 76 | "Filename" => "Varchar(255)", |
||
| 77 | "Name" => "Varchar(255)", |
||
| 78 | "URL" => "Varchar(255)", |
||
| 79 | "BackendIdentifier" => "Varchar(255)", // Injector identifier of the DeploymentBackend |
||
| 80 | "Usage" => "Enum('Production, UAT, Test, Unspecified', 'Unspecified')" |
||
| 81 | ); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | public static $has_one = array( |
||
| 87 | "Project" => "DNProject", |
||
| 88 | "CreateEnvironment" => "DNCreateEnvironment" |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | public static $has_many = array( |
||
| 95 | "Deployments" => "DNDeployment", |
||
| 96 | "DataArchives" => "DNDataArchive", |
||
| 97 | ); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | public static $many_many = array( |
||
| 103 | "Viewers" => "Member", // Who can view this environment |
||
| 104 | "ViewerGroups" => "Group", |
||
| 105 | "Deployers" => "Member", // Who can deploy to this environment |
||
| 106 | "DeployerGroups" => "Group", |
||
| 107 | "CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
||
| 108 | "CanRestoreGroups" => "Group", |
||
| 109 | "CanBackupMembers" => "Member", // Who can backup archive files from this environment |
||
| 110 | "CanBackupGroups" => "Group", |
||
| 111 | "ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
||
| 112 | "ArchiveUploaderGroups" => "Group", |
||
| 113 | "ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
||
| 114 | "ArchiveDownloaderGroups" => "Group", |
||
| 115 | "ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
||
| 116 | "ArchiveDeleterGroups" => "Group", |
||
| 117 | ); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | public static $summary_fields = array( |
||
| 123 | "Name" => "Environment Name", |
||
| 124 | "Usage" => "Usage", |
||
| 125 | "URL" => "URL", |
||
| 126 | "DeployersList" => "Can Deploy List", |
||
| 127 | "CanRestoreMembersList" => "Can Restore List", |
||
| 128 | "CanBackupMembersList" => "Can Backup List", |
||
| 129 | "ArchiveUploadersList" => "Can Upload List", |
||
| 130 | "ArchiveDownloadersList" => "Can Download List", |
||
| 131 | "ArchiveDeletersList" => "Can Delete List", |
||
| 132 | ); |
||
| 133 | |||
| 134 | private static $singular_name = 'Capistrano Environment'; |
||
| 135 | |||
| 136 | private static $plural_name = 'Capistrano Environments'; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | public static $searchable_fields = array( |
||
| 142 | "Name", |
||
| 143 | ); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | private static $default_sort = 'Name'; |
||
| 149 | |||
| 150 | const UAT = 'UAT'; |
||
| 151 | |||
| 152 | const PRODUCTION = 'Production'; |
||
| 153 | |||
| 154 | const UNSPECIFIED = 'Unspecified'; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Used by the sync task |
||
| 158 | * |
||
| 159 | * @param string $path |
||
| 160 | * @return \DNEnvironment |
||
| 161 | */ |
||
| 162 | public static function create_from_path($path) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the deployment backend used for this environment. |
||
| 175 | * |
||
| 176 | * Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is |
||
| 177 | * illegal then that value is ignored. |
||
| 178 | * |
||
| 179 | * @return DeploymentBackend |
||
| 180 | */ |
||
| 181 | public function Backend() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param SS_HTTPRequest $request |
||
| 207 | * |
||
| 208 | * @return DeploymentStrategy |
||
| 209 | */ |
||
| 210 | public function getDeployStrategy(\SS_HTTPRequest $request) { |
||
| 211 | return $this->Backend()->planDeploy($this, $request->requestVars()); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function Menu() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Return the current object from $this->Menu() |
||
| 234 | * Good for making titles and things |
||
| 235 | */ |
||
| 236 | public function CurrentMenu() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return a name for this environment. |
||
| 242 | * |
||
| 243 | * @param string $separator The string used when concatenating project with env name |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getFullName($separator = ':') { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * URL for the environment that can be used if no explicit URL is set. |
||
| 252 | */ |
||
| 253 | public function getDefaultURL() { |
||
| 254 | return null; |
||
| 255 | } |
||
| 256 | |||
| 257 | public function getBareURL() { |
||
| 263 | |||
| 264 | public function getBareDefaultURL() { |
||
| 265 | $url = parse_url($this->getDefaultURL()); |
||
| 266 | if(isset($url['host'])) { |
||
| 267 | return strtolower($url['host']); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Environments are only viewable by people that can view the environment. |
||
| 273 | * |
||
| 274 | * @param Member|null $member |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | public function canView($member = null) { |
||
| 278 | if(!$member) { |
||
| 279 | $member = Member::currentUser(); |
||
| 280 | } |
||
| 281 | if(!$member) { |
||
| 282 | return false; |
||
| 283 | } |
||
| 284 | // Must be logged in to check permissions |
||
| 285 | |||
| 286 | if(Permission::checkMember($member, 'ADMIN')) { |
||
| 287 | return true; |
||
| 288 | } |
||
| 289 | |||
| 290 | // if no Viewers or ViewerGroups defined, fallback to DNProject::canView permissions |
||
| 291 | if($this->Viewers()->exists() || $this->ViewerGroups()->exists()) { |
||
| 292 | return $this->Viewers()->byID($member->ID) |
||
| 293 | || $member->inGroups($this->ViewerGroups()); |
||
| 294 | } |
||
| 295 | |||
| 296 | return $this->Project()->canView($member); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Allow deploy only to some people. |
||
| 301 | * |
||
| 302 | * @param Member|null $member |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | View Code Duplication | public function canDeploy($member = null) { |
|
| 306 | if(!$member) { |
||
| 307 | $member = Member::currentUser(); |
||
| 308 | } |
||
| 309 | if(!$member) { |
||
| 310 | return false; |
||
| 311 | } |
||
| 312 | // Must be logged in to check permissions |
||
| 313 | |||
| 314 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
| 315 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_DEPLOYMENT, $member)) return true; |
||
| 316 | } else { |
||
| 317 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_DEPLOYMENT, $member)) return true; |
||
| 318 | } |
||
| 319 | |||
| 320 | return $this->Deployers()->byID($member->ID) |
||
| 321 | || $member->inGroups($this->DeployerGroups()); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Provide reason why the user cannot deploy. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getCannotDeployMessage() { |
||
| 330 | return 'You cannot deploy to this environment.'; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
||
| 335 | * {@link DNEnvironment}. |
||
| 336 | * |
||
| 337 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 338 | * @return boolean true if $member can restore, and false if they can't. |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function canRestore($member = null) { |
|
| 341 | if(!$member) { |
||
| 342 | $member = Member::currentUser(); |
||
| 343 | } |
||
| 344 | if(!$member) { |
||
| 345 | return false; |
||
| 346 | } |
||
| 347 | // Must be logged in to check permissions |
||
| 348 | |||
| 349 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
| 350 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) return true; |
||
| 351 | } else { |
||
| 352 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) return true; |
||
| 353 | } |
||
| 354 | |||
| 355 | return $this->CanRestoreMembers()->byID($member->ID) |
||
| 356 | || $member->inGroups($this->CanRestoreGroups()); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
||
| 361 | * file. |
||
| 362 | * |
||
| 363 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 364 | * @return boolean true if $member can backup, and false if they can't. |
||
| 365 | */ |
||
| 366 | View Code Duplication | public function canBackup($member = null) { |
|
| 367 | $project = $this->Project(); |
||
| 368 | if($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
||
| 369 | return false; |
||
| 370 | } |
||
| 371 | |||
| 372 | if(!$member) { |
||
| 373 | $member = Member::currentUser(); |
||
| 374 | } |
||
| 375 | // Must be logged in to check permissions |
||
| 376 | if(!$member) { |
||
| 377 | return false; |
||
| 378 | } |
||
| 379 | |||
| 380 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
| 381 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) return true; |
||
| 382 | } else { |
||
| 383 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) return true; |
||
| 384 | } |
||
| 385 | |||
| 386 | return $this->CanBackupMembers()->byID($member->ID) |
||
| 387 | || $member->inGroups($this->CanBackupGroups()); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
||
| 392 | * {@link DNEnvironment}. |
||
| 393 | * |
||
| 394 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
||
| 395 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
||
| 396 | * {@link self::canRestore()}. |
||
| 397 | * |
||
| 398 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 399 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
| 400 | */ |
||
| 401 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
||
| 427 | * {@link DNEnvironment}. |
||
| 428 | * |
||
| 429 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 430 | * @return boolean true if $member can download archives from this environment, false if they can't. |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
||
| 453 | * {@link DNEnvironment}. |
||
| 454 | * |
||
| 455 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 456 | * @return boolean true if $member can delete archives from this environment, false if they can't. |
||
| 457 | */ |
||
| 458 | View Code Duplication | public function canDeleteArchive($member = null) { |
|
| 476 | /** |
||
| 477 | * Get a string of groups/people that are allowed to deploy to this environment. |
||
| 478 | * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public function getDeployersList() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | public function getCanRestoreMembersList() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getCanBackupMembersList() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
||
| 524 | * objects linked to this environment. |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function getArchiveUploadersList() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function getArchiveDownloadersList() { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
||
| 555 | * |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | public function getArchiveDeletersList() { |
||
| 559 | return implode( |
||
| 560 | ", ", |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @return DNData |
||
| 570 | */ |
||
| 571 | public function DNData() { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Get the current deployed build for this environment |
||
| 577 | * |
||
| 578 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
||
| 579 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
||
| 580 | * |
||
| 581 | * @return false|DNDeployment |
||
| 582 | */ |
||
| 583 | public function CurrentBuild() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * A list of past deployments. |
||
| 618 | * @return ArrayList |
||
| 619 | */ |
||
| 620 | View Code Duplication | public function DeployHistory() { |
|
| 630 | |||
| 631 | /** |
||
| 632 | * A list of upcoming or current deployments. |
||
| 633 | * @return ArrayList |
||
| 634 | */ |
||
| 635 | View Code Duplication | public function UpcomingDeployments() { |
|
| 645 | |||
| 646 | /** |
||
| 647 | * @param string $sha |
||
| 648 | * @return array |
||
| 649 | */ |
||
| 650 | protected function getCommitData($sha) { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param string $action |
||
| 677 | * |
||
| 678 | * @return string |
||
| 679 | */ |
||
| 680 | public function Link($action = '') { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Is this environment currently at the root level of the controller that handles it? |
||
| 686 | * @return bool |
||
| 687 | */ |
||
| 688 | public function isCurrent() { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | public function isSection() { |
||
| 701 | |||
| 702 | |||
| 703 | /** |
||
| 704 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
| 705 | * |
||
| 706 | * @param string $groupField Group field name |
||
| 707 | * @param string $memberField Member field name |
||
| 708 | * @param array $groups List of groups |
||
| 709 | * @param array $members List of members |
||
| 710 | * @return FieldGroup |
||
| 711 | */ |
||
| 712 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * @return FieldList |
||
| 730 | */ |
||
| 731 | public function getCMSFields() { |
||
| 881 | |||
| 882 | /** |
||
| 883 | * @param FieldList $fields |
||
| 884 | */ |
||
| 885 | protected function setDeployConfigurationFields(&$fields) { |
||
| 905 | |||
| 906 | /** |
||
| 907 | */ |
||
| 908 | public function onBeforeWrite() { |
||
| 916 | |||
| 917 | public function onAfterWrite() { |
||
| 932 | |||
| 933 | |||
| 934 | /** |
||
| 935 | * Ensure that environment paths are setup on the local filesystem |
||
| 936 | */ |
||
| 937 | protected function checkEnvironmentPath() { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Write the deployment config file to filesystem |
||
| 947 | */ |
||
| 948 | protected function writeConfigFile() { |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Delete any related config files |
||
| 967 | */ |
||
| 968 | public function onAfterDelete() { |
||
| 980 | |||
| 981 | /** |
||
| 982 | * @return string |
||
| 983 | */ |
||
| 984 | protected function getEnvironmentConfig() { |
||
| 990 | |||
| 991 | /** |
||
| 992 | * @return boolean |
||
| 993 | */ |
||
| 994 | protected function envFileExists() { |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Returns the path to the ruby config file |
||
| 1003 | * |
||
| 1004 | * @return string |
||
| 1005 | */ |
||
| 1006 | public function getConfigFilename() { |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
| 1018 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
| 1019 | * |
||
| 1020 | * @param array $array The (single- or multi-dimensional) array to convert |
||
| 1021 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
| 1022 | * isn't an array. |
||
| 1023 | */ |
||
| 1024 | public static function array_to_viewabledata($array) { |
||
| 1049 | |||
| 1050 | protected function validate() { |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
| 1063 | * if an old deployment is stuck. |
||
| 1064 | * |
||
| 1065 | * @return DataList |
||
| 1066 | */ |
||
| 1067 | public function runningDeployments() { |
||
| 1079 | |||
| 1080 | } |
||
| 1081 |
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.