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 Subsite 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 Subsite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Subsite extends DataObject |
||
| 47 | { |
||
| 48 | |||
| 49 | private static $table_name = 'Subsite'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var boolean $disable_subsite_filter If enabled, bypasses the query decoration |
||
| 53 | * to limit DataObject::get*() calls to a specific subsite. Useful for debugging. |
||
| 54 | */ |
||
| 55 | public static $disable_subsite_filter = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Allows you to force a specific subsite ID, or comma separated list of IDs. |
||
| 59 | * Only works for reading. An object cannot be written to more than 1 subsite. |
||
| 60 | */ |
||
| 61 | public static $force_subsite = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * |
||
| 65 | * @var boolean |
||
| 66 | */ |
||
| 67 | public static $write_hostmap = true; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Memory cache of accessible sites |
||
| 71 | * |
||
| 72 | * @array |
||
| 73 | */ |
||
| 74 | private static $_cache_accessible_sites = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Memory cache of subsite id for domains |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private static $_cache_subsite_for_domain = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array $allowed_themes Numeric array of all themes which are allowed to be selected for all subsites. |
||
| 85 | * Corresponds to subfolder names within the /themes folder. By default, all themes contained in this folder |
||
| 86 | * are listed. |
||
| 87 | */ |
||
| 88 | private static $allowed_themes = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var Boolean If set to TRUE, don't assume 'www.example.com' and 'example.com' are the same. |
||
| 92 | * Doesn't affect wildcard matching, so '*.example.com' will match 'www.example.com' (but not 'example.com') |
||
| 93 | * in both TRUE or FALSE setting. |
||
| 94 | */ |
||
| 95 | public static $strict_subdomain_matching = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var boolean Respects the IsPublic flag when retrieving subsites |
||
| 99 | */ |
||
| 100 | public static $check_is_public = true; |
||
| 101 | |||
| 102 | /*** @return array |
||
| 103 | */ |
||
| 104 | private static $summary_fields = [ |
||
| 105 | 'Title', |
||
| 106 | 'PrimaryDomain', |
||
| 107 | 'IsPublic' |
||
| 108 | ]; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | private static $db = [ |
||
| 114 | 'Title' => 'Varchar(255)', |
||
| 115 | 'RedirectURL' => 'Varchar(255)', |
||
| 116 | 'DefaultSite' => 'Boolean', |
||
| 117 | 'Theme' => 'Varchar', |
||
| 118 | 'Language' => 'Varchar(6)', |
||
| 119 | |||
| 120 | // Used to hide unfinished/private subsites from public view. |
||
| 121 | // If unset, will default to true |
||
| 122 | 'IsPublic' => 'Boolean', |
||
| 123 | |||
| 124 | // Comma-separated list of disallowed page types |
||
| 125 | 'PageTypeBlacklist' => 'Text', |
||
| 126 | ]; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | private static $has_many = [ |
||
| 132 | 'Domains' => SubsiteDomain::class, |
||
| 133 | ]; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | private static $belongs_many_many = [ |
||
| 139 | 'Groups' => Group::class, |
||
| 140 | ]; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | private static $defaults = [ |
||
| 146 | 'IsPublic' => 1 |
||
| 147 | ]; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | private static $searchable_fields = [ |
||
| 153 | 'Title', |
||
| 154 | 'Domains.Domain', |
||
| 155 | 'IsPublic', |
||
| 156 | ]; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | private static $default_sort = '"Title" ASC'; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set allowed themes |
||
| 165 | * |
||
| 166 | * @param array $themes - Numeric array of all themes which are allowed to be selected for all subsites. |
||
| 167 | */ |
||
| 168 | public static function set_allowed_themes($themes) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Gets the subsite currently set in the session. |
||
| 175 | * |
||
| 176 | * @uses ControllerSubsites->controllerAugmentInit() |
||
| 177 | * @return DataObject The current Subsite |
||
| 178 | */ |
||
| 179 | public static function currentSubsite() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * This function gets the current subsite ID from the session. It used in the backend so Ajax requests |
||
| 186 | * use the correct subsite. The frontend handles subsites differently. It calls getSubsiteIDForDomain |
||
| 187 | * directly from ModelAsController::getNestedController. Only gets Subsite instances which have their |
||
| 188 | * {@link IsPublic} flag set to TRUE. |
||
| 189 | * |
||
| 190 | * You can simulate subsite access without creating virtual hosts by appending ?SubsiteID=<ID> to the request. |
||
| 191 | * |
||
| 192 | * @return int ID of the current subsite instance |
||
| 193 | * |
||
| 194 | * @deprecated 2.0..3.0 Use SubsiteState::singleton()->getSubsiteId() instead |
||
| 195 | */ |
||
| 196 | public static function currentSubsiteID() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Switch to another subsite through storing the subsite identifier in the current PHP session. |
||
| 204 | * Only takes effect when {@link SubsiteState::singleton()->getUseSessions()} is set to TRUE. |
||
| 205 | * |
||
| 206 | * @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself |
||
| 207 | */ |
||
| 208 | public static function changeSubsite($subsite) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get a matching subsite for the given host, or for the current HTTP_HOST. |
||
| 237 | * Supports "fuzzy" matching of domains by placing an asterisk at the start of end of the string, |
||
| 238 | * for example matching all subdomains on *.example.com with one subsite, |
||
| 239 | * and all subdomains on *.example.org on another. |
||
| 240 | * |
||
| 241 | * @param $host string The host to find the subsite for. If not specified, $_SERVER['HTTP_HOST'] is used. |
||
| 242 | * @param bool $checkPermissions |
||
| 243 | * @return int Subsite ID |
||
| 244 | */ |
||
| 245 | public static function getSubsiteIDForDomain($host = null, $checkPermissions = true) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * |
||
| 314 | * @param string $className |
||
| 315 | * @param string $filter |
||
| 316 | * @param string $sort |
||
| 317 | * @param string $join |
||
| 318 | * @param string $limit |
||
| 319 | * @return DataList |
||
| 320 | */ |
||
| 321 | public static function get_from_all_subsites($className, $filter = '', $sort = '', $join = '', $limit = '') |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Disable the sub-site filtering; queries will select from all subsites |
||
| 330 | * @param bool $disabled |
||
| 331 | */ |
||
| 332 | public static function disable_subsite_filter($disabled = true) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Flush caches on database reset |
||
| 339 | */ |
||
| 340 | public static function on_db_reset() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Return all subsites, regardless of permissions (augmented with main site). |
||
| 348 | * |
||
| 349 | * @param bool $includeMainSite |
||
| 350 | * @param string $mainSiteTitle |
||
| 351 | * @return SS_List List of <a href='psi_element://Subsite'>Subsite</a> objects (DataList or ArrayList). |
||
| 352 | * objects (DataList or ArrayList). |
||
| 353 | */ |
||
| 354 | public static function all_sites($includeMainSite = true, $mainSiteTitle = 'Main site') |
||
| 370 | |||
| 371 | /* |
||
| 372 | * Returns an ArrayList of the subsites accessible to the current user. |
||
| 373 | * It's enough for any section to be accessible for the site to be included. |
||
| 374 | * |
||
| 375 | * @return ArrayList of {@link Subsite} instances. |
||
| 376 | */ |
||
| 377 | public static function all_accessible_sites($includeMainSite = true, $mainSiteTitle = 'Main site', $member = null) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Return the subsites that the current user can access by given permission. |
||
| 414 | * Sites will only be included if they have a Title. |
||
| 415 | * |
||
| 416 | * @param $permCode array|string Either a single permission code or an array of permission codes. |
||
| 417 | * @param $includeMainSite bool If true, the main site will be included if appropriate. |
||
| 418 | * @param $mainSiteTitle string The label to give to the main site |
||
| 419 | * @param $member int|Member The member attempting to access the sites |
||
| 420 | * @return DataList|ArrayList of {@link Subsite} instances |
||
| 421 | */ |
||
| 422 | public static function accessible_sites( |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Write a host->domain map to subsites/host-map.php |
||
| 529 | * |
||
| 530 | * This is used primarily when using subsites in conjunction with StaticPublisher |
||
| 531 | * |
||
| 532 | * @param string $file - filepath of the host map to be written |
||
| 533 | * @return void |
||
| 534 | */ |
||
| 535 | public static function writeHostMap($file = null) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Checks if a member can be granted certain permissions, regardless of the subsite context. |
||
| 577 | * Similar logic to {@link Permission::checkMember()}, but only returns TRUE |
||
| 578 | * if the member is part of a group with the "AccessAllSubsites" flag set. |
||
| 579 | * If more than one permission is passed to the method, at least one of them must |
||
| 580 | * be granted for if to return TRUE. |
||
| 581 | * |
||
| 582 | * @todo Allow permission inheritance through group hierarchy. |
||
| 583 | * |
||
| 584 | * @param Member Member to check against. Defaults to currently logged in member |
||
| 585 | * @param array $permissionCodes |
||
| 586 | * @return bool |
||
| 587 | */ |
||
| 588 | public static function hasMainSitePermission($member = null, $permissionCodes = ['ADMIN']) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @todo Possible security issue, don't grant edit permissions to everybody. |
||
| 639 | * @param bool $member |
||
| 640 | * @return bool |
||
| 641 | */ |
||
| 642 | public function canEdit($member = false) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Show the configuration fields for each subsite |
||
| 649 | * |
||
| 650 | * @return FieldList |
||
| 651 | */ |
||
| 652 | public function getCMSFields() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Return a list of the different page types available to the CMS |
||
| 716 | * |
||
| 717 | * @return array |
||
| 718 | */ |
||
| 719 | public function getPageTypeMap() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * |
||
| 735 | * @param boolean $includerelations |
||
| 736 | * @return array |
||
| 737 | */ |
||
| 738 | public function fieldLabels($includerelations = true) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Return the themes that can be used with this subsite, as an array of themecode => description |
||
| 756 | * |
||
| 757 | * @return array |
||
| 758 | */ |
||
| 759 | public function allowedThemes() |
||
| 778 | |||
| 779 | /** |
||
| 780 | * @return string Current locale of the subsite |
||
| 781 | */ |
||
| 782 | public function getLanguage() |
||
| 790 | |||
| 791 | /** |
||
| 792 | * |
||
| 793 | * @return \SilverStripe\ORM\ValidationResult |
||
| 794 | */ |
||
| 795 | public function validate() |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Whenever a Subsite is written, rewrite the hostmap |
||
| 806 | * |
||
| 807 | * @return void |
||
| 808 | */ |
||
| 809 | public function onAfterWrite() |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Return the primary domain of this site. Tries to "normalize" the domain name, |
||
| 817 | * by replacing potential wildcards. |
||
| 818 | * |
||
| 819 | * @return string The full domain name of this subsite (without protocol prefix) |
||
| 820 | */ |
||
| 821 | public function domain() |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Finds the primary {@see SubsiteDomain} object for this subsite |
||
| 835 | * |
||
| 836 | * @return SubsiteDomain |
||
| 837 | */ |
||
| 838 | public function getPrimarySubsiteDomain() |
||
| 845 | |||
| 846 | /** |
||
| 847 | * |
||
| 848 | * @return string - The full domain name of this subsite (without protocol prefix) |
||
| 849 | */ |
||
| 850 | public function getPrimaryDomain() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Get the absolute URL for this subsite |
||
| 857 | * @return string |
||
| 858 | */ |
||
| 859 | public function absoluteBaseURL() |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Javascript admin action to duplicate this subsite |
||
| 873 | * |
||
| 874 | * @return string - javascript |
||
| 875 | */ |
||
| 876 | public function adminDuplicate() |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Make this subsite the current one |
||
| 893 | */ |
||
| 894 | public function activate() |
||
| 898 | |||
| 899 | /** |
||
| 900 | * |
||
| 901 | * @param array $permissionCodes |
||
| 902 | * @return DataList |
||
| 903 | */ |
||
| 904 | public function getMembersByPermission($permissionCodes = ['ADMIN']) |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Duplicate this subsite |
||
| 925 | * @param bool $doWrite |
||
| 926 | * @param string $manyMany |
||
| 927 | * @return DataObject |
||
| 928 | */ |
||
| 929 | public function duplicate($doWrite = true, $manyMany = 'many_many') |
||
| 967 | } |
||
| 968 |