| Total Complexity | 56 |
| Total Lines | 457 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SiteConfig 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.
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 SiteConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class SiteConfig extends DataObject implements PermissionProvider, TemplateGlobalProvider |
||
| 39 | { |
||
| 40 | private static $db = [ |
||
| 41 | "Title" => "Varchar(255)", |
||
| 42 | "Tagline" => "Varchar(255)", |
||
| 43 | "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')", |
||
| 44 | "CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')", |
||
| 45 | "CanCreateTopLevelType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')", |
||
| 46 | ]; |
||
| 47 | |||
| 48 | private static $many_many = [ |
||
| 49 | "ViewerGroups" => Group::class, |
||
| 50 | "EditorGroups" => Group::class, |
||
| 51 | "CreateTopLevelGroups" => Group::class, |
||
| 52 | ]; |
||
| 53 | |||
| 54 | private static $defaults = [ |
||
| 55 | "CanViewType" => "Anyone", |
||
| 56 | "CanEditType" => "LoggedInUsers", |
||
| 57 | "CanCreateTopLevelType" => "LoggedInUsers", |
||
| 58 | ]; |
||
| 59 | |||
| 60 | private static $table_name = 'SiteConfig'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Default permission to check for 'LoggedInUsers' to create or edit pages |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | * @config |
||
| 67 | */ |
||
| 68 | private static $required_permission = [ |
||
| 69 | 'CMS_ACCESS_CMSMain', |
||
| 70 | 'CMS_ACCESS_LeftAndMain' |
||
| 71 | ]; |
||
| 72 | |||
| 73 | public function populateDefaults() |
||
| 74 | { |
||
| 75 | $this->Title = _t(self::class . '.SITENAMEDEFAULT', "Your Site Name"); |
||
| 76 | $this->Tagline = _t(self::class . '.TAGLINEDEFAULT', "your tagline here"); |
||
| 77 | |||
| 78 | // Allow these defaults to be overridden |
||
| 79 | parent::populateDefaults(); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the fields that are sent to the CMS. |
||
| 84 | * |
||
| 85 | * In your extensions: updateCMSFields($fields). |
||
| 86 | * |
||
| 87 | * @return FieldList |
||
| 88 | */ |
||
| 89 | public function getCMSFields() |
||
| 90 | { |
||
| 91 | $mapFn = function ($groups = []) { |
||
| 92 | $map = []; |
||
| 93 | foreach ($groups as $group) { |
||
| 94 | // Listboxfield values are escaped, use ASCII char instead of » |
||
| 95 | $map[$group->ID] = $group->getBreadcrumbs(' > '); |
||
| 96 | } |
||
| 97 | asort($map); |
||
| 98 | return $map; |
||
| 99 | }; |
||
| 100 | $groupsMap = $mapFn(Group::get()); |
||
| 101 | $viewAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_VIEW_ALL', 'ADMIN'])); |
||
| 102 | $editAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_EDIT_ALL', 'ADMIN'])); |
||
| 103 | |||
| 104 | $fields = FieldList::create( |
||
| 105 | TabSet::create( |
||
| 106 | "Root", |
||
| 107 | $tabMain = Tab::create( |
||
| 108 | 'Main', |
||
| 109 | $titleField = TextField::create("Title", _t(self::class . '.SITETITLE', "Site title")), |
||
| 110 | $taglineField = TextField::create( |
||
| 111 | "Tagline", |
||
| 112 | _t(self::class . '.SITETAGLINE', "Site Tagline/Slogan") |
||
| 113 | ) |
||
| 114 | ), |
||
| 115 | $tabAccess = Tab::create( |
||
| 116 | 'Access', |
||
| 117 | $viewersOptionsField = OptionsetField::create( |
||
| 118 | "CanViewType", |
||
| 119 | _t(self::class . '.VIEWHEADER', "Who can view pages on this site?") |
||
| 120 | ), |
||
| 121 | $viewerGroupsField = ListboxField::create( |
||
| 122 | "ViewerGroups", |
||
| 123 | _t('SilverStripe\\CMS\\Model\\SiteTree.VIEWERGROUPS', "Viewer Groups") |
||
| 124 | ) |
||
| 125 | ->setSource($groupsMap) |
||
| 126 | ->setAttribute( |
||
| 127 | 'data-placeholder', |
||
| 128 | _t('SilverStripe\\CMS\\Model\\SiteTree.GroupPlaceholder', 'Click to select group') |
||
| 129 | ), |
||
| 130 | $editorsOptionsField = OptionsetField::create( |
||
| 131 | "CanEditType", |
||
| 132 | _t(self::class . '.EDITHEADER', "Who can edit pages on this site?") |
||
| 133 | ), |
||
| 134 | $editorGroupsField = ListboxField::create( |
||
| 135 | "EditorGroups", |
||
| 136 | _t('SilverStripe\\CMS\\Model\\SiteTree.EDITORGROUPS', "Editor Groups") |
||
| 137 | ) |
||
| 138 | ->setSource($groupsMap) |
||
| 139 | ->setAttribute( |
||
| 140 | 'data-placeholder', |
||
| 141 | _t('SilverStripe\\CMS\\Model\\SiteTree.GroupPlaceholder', 'Click to select group') |
||
| 142 | ), |
||
| 143 | $topLevelCreatorsOptionsField = OptionsetField::create( |
||
| 144 | "CanCreateTopLevelType", |
||
| 145 | _t(self::class . '.TOPLEVELCREATE', "Who can create pages in the root of the site?") |
||
| 146 | ), |
||
| 147 | $topLevelCreatorsGroupsField = ListboxField::create( |
||
| 148 | "CreateTopLevelGroups", |
||
| 149 | _t(self::class . '.TOPLEVELCREATORGROUPS', "Top level creators") |
||
| 150 | ) |
||
| 151 | ->setSource($groupsMap) |
||
| 152 | ->setAttribute( |
||
| 153 | 'data-placeholder', |
||
| 154 | _t('SilverStripe\\CMS\\Model\\SiteTree.GroupPlaceholder', 'Click to select group') |
||
| 155 | ) |
||
| 156 | ) |
||
| 157 | ), |
||
| 158 | HiddenField::create('ID') |
||
| 159 | ); |
||
| 160 | |||
| 161 | $viewersOptionsSource = []; |
||
| 162 | $viewersOptionsSource["Anyone"] = _t('SilverStripe\\CMS\\Model\\SiteTree.ACCESSANYONE', "Anyone"); |
||
| 163 | $viewersOptionsSource["LoggedInUsers"] = _t( |
||
| 164 | 'SilverStripe\\CMS\\Model\\SiteTree.ACCESSLOGGEDIN', |
||
| 165 | "Logged-in users" |
||
| 166 | ); |
||
| 167 | $viewersOptionsSource["OnlyTheseUsers"] = _t( |
||
| 168 | 'SilverStripe\\CMS\\Model\\SiteTree.ACCESSONLYTHESE', |
||
| 169 | "Only these groups (choose from list)" |
||
| 170 | ); |
||
| 171 | $viewersOptionsField->setSource($viewersOptionsSource); |
||
| 172 | |||
| 173 | if ($viewAllGroupsMap) { |
||
| 174 | $viewerGroupsField->setDescription(_t( |
||
| 175 | 'SilverStripe\\CMS\\Model\\SiteTree.VIEWER_GROUPS_FIELD_DESC', |
||
| 176 | 'Groups with global view permissions: {groupList}', |
||
| 177 | ['groupList' => implode(', ', array_values($viewAllGroupsMap))] |
||
| 178 | )); |
||
| 179 | } |
||
| 180 | |||
| 181 | if ($editAllGroupsMap) { |
||
| 182 | $editorGroupsField->setDescription(_t( |
||
| 183 | 'SilverStripe\\CMS\\Model\\SiteTree.EDITOR_GROUPS_FIELD_DESC', |
||
| 184 | 'Groups with global edit permissions: {groupList}', |
||
| 185 | ['groupList' => implode(', ', array_values($editAllGroupsMap))] |
||
| 186 | )); |
||
| 187 | } |
||
| 188 | |||
| 189 | $editorsOptionsSource = []; |
||
| 190 | $editorsOptionsSource["LoggedInUsers"] = _t( |
||
| 191 | 'SilverStripe\\CMS\\Model\\SiteTree.EDITANYONE', |
||
| 192 | "Anyone who can log-in to the CMS" |
||
| 193 | ); |
||
| 194 | $editorsOptionsSource["OnlyTheseUsers"] = _t( |
||
| 195 | 'SilverStripe\\CMS\\Model\\SiteTree.EDITONLYTHESE', |
||
| 196 | "Only these groups (choose from list)" |
||
| 197 | ); |
||
| 198 | $editorsOptionsField->setSource($editorsOptionsSource); |
||
| 199 | |||
| 200 | $topLevelCreatorsOptionsField->setSource($editorsOptionsSource); |
||
| 201 | |||
| 202 | if (!Permission::check('EDIT_SITECONFIG')) { |
||
| 203 | $fields->makeFieldReadonly($viewersOptionsField); |
||
| 204 | $fields->makeFieldReadonly($viewerGroupsField); |
||
| 205 | $fields->makeFieldReadonly($editorsOptionsField); |
||
| 206 | $fields->makeFieldReadonly($editorGroupsField); |
||
| 207 | $fields->makeFieldReadonly($topLevelCreatorsOptionsField); |
||
| 208 | $fields->makeFieldReadonly($topLevelCreatorsGroupsField); |
||
| 209 | $fields->makeFieldReadonly($taglineField); |
||
| 210 | $fields->makeFieldReadonly($titleField); |
||
| 211 | } |
||
| 212 | |||
| 213 | if (file_exists(BASE_PATH . '/install.php')) { |
||
| 214 | $fields->addFieldToTab( |
||
| 215 | 'Root.Main', |
||
| 216 | LiteralField::create( |
||
| 217 | 'InstallWarningHeader', |
||
| 218 | '<div class="alert alert-warning">' . _t( |
||
| 219 | 'SilverStripe\\CMS\\Model\\SiteTree.REMOVE_INSTALL_WARNING', |
||
| 220 | 'Warning: You should remove install.php from this SilverStripe install for security reasons.' |
||
| 221 | ) . '</div>' |
||
| 222 | ), |
||
| 223 | 'Title' |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | $tabMain->setTitle(_t(self::class . '.TABMAIN', "Main")); |
||
| 228 | $tabAccess->setTitle(_t(self::class . '.TABACCESS', "Access")); |
||
| 229 | $this->extend('updateCMSFields', $fields); |
||
| 230 | |||
| 231 | return $fields; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the actions that are sent to the CMS. |
||
| 236 | * |
||
| 237 | * In your extensions: updateEditFormActions($actions) |
||
| 238 | * |
||
| 239 | * @return FieldList |
||
| 240 | */ |
||
| 241 | public function getCMSActions() |
||
| 242 | { |
||
| 243 | if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) { |
||
| 244 | $actions = FieldList::create( |
||
| 245 | FormAction::create( |
||
| 246 | 'save_siteconfig', |
||
| 247 | _t('SilverStripe\\CMS\\Controllers\\CMSMain.SAVE', 'Save') |
||
| 248 | )->addExtraClass('btn-primary font-icon-save') |
||
| 249 | ); |
||
| 250 | } else { |
||
| 251 | $actions = FieldList::create(); |
||
| 252 | } |
||
| 253 | |||
| 254 | $this->extend('updateCMSActions', $actions); |
||
| 255 | |||
| 256 | return $actions; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function CMSEditLink() |
||
| 263 | { |
||
| 264 | return SiteConfigLeftAndMain::singleton()->Link(); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the current sites SiteConfig, and creates a new one through |
||
| 269 | * {@link make_site_config()} if none is found. |
||
| 270 | * |
||
| 271 | * @return SiteConfig |
||
| 272 | */ |
||
| 273 | public static function current_site_config() |
||
| 274 | { |
||
| 275 | /** @var SiteConfig $siteConfig */ |
||
| 276 | $siteConfig = DataObject::get_one(SiteConfig::class); |
||
| 277 | if (!$siteConfig) { |
||
| 278 | $siteConfig = self::make_site_config(); |
||
| 279 | } |
||
| 280 | |||
| 281 | static::singleton()->extend('updateCurrentSiteConfig', $siteConfig); |
||
| 282 | |||
| 283 | return $siteConfig; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Setup a default SiteConfig record if none exists. |
||
| 288 | */ |
||
| 289 | public function requireDefaultRecords() |
||
| 290 | { |
||
| 291 | parent::requireDefaultRecords(); |
||
| 292 | |||
| 293 | $config = DataObject::get_one(SiteConfig::class); |
||
| 294 | |||
| 295 | if (!$config) { |
||
| 296 | self::make_site_config(); |
||
| 297 | |||
| 298 | DB::alteration_message("Added default site config", "created"); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Create SiteConfig with defaults from language file. |
||
| 304 | * |
||
| 305 | * @return SiteConfig |
||
| 306 | */ |
||
| 307 | public static function make_site_config() |
||
| 308 | { |
||
| 309 | $config = SiteConfig::create(); |
||
| 310 | $config->write(); |
||
| 311 | |||
| 312 | return $config; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Can a user view this SiteConfig instance? |
||
| 317 | * |
||
| 318 | * @param Member $member |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | public function canView($member = null) |
||
| 322 | { |
||
| 323 | if (!$member) { |
||
| 324 | $member = Security::getCurrentUser(); |
||
| 325 | } |
||
| 326 | |||
| 327 | $extended = $this->extendedCan('canView', $member); |
||
| 328 | if ($extended !== null) { |
||
| 329 | return $extended; |
||
| 330 | } |
||
| 331 | |||
| 332 | // Assuming all that can edit this object can also view it |
||
| 333 | return $this->canEdit($member); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Can a user view pages on this site? This method is only |
||
| 338 | * called if a page is set to Inherit, but there is nothing |
||
| 339 | * to inherit from. |
||
| 340 | * |
||
| 341 | * @param Member $member |
||
| 342 | * @return boolean |
||
| 343 | */ |
||
| 344 | public function canViewPages($member = null) |
||
| 345 | { |
||
| 346 | if (!$member) { |
||
| 347 | $member = Security::getCurrentUser(); |
||
| 348 | } |
||
| 349 | |||
| 350 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
| 351 | return true; |
||
| 352 | } |
||
| 353 | |||
| 354 | $extended = $this->extendedCan('canViewPages', $member); |
||
| 355 | if ($extended !== null) { |
||
| 356 | return $extended; |
||
| 357 | } |
||
| 358 | |||
| 359 | if (!$this->CanViewType || $this->CanViewType == 'Anyone') { |
||
| 360 | return true; |
||
| 361 | } |
||
| 362 | |||
| 363 | // check for any logged-in users |
||
| 364 | if ($this->CanViewType === 'LoggedInUsers' && $member) { |
||
| 365 | return true; |
||
| 366 | } |
||
| 367 | |||
| 368 | // check for specific groups |
||
| 369 | if ($this->CanViewType === 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) { |
||
| 370 | return true; |
||
| 371 | } |
||
| 372 | |||
| 373 | return false; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Can a user edit pages on this site? This method is only |
||
| 378 | * called if a page is set to Inherit, but there is nothing |
||
| 379 | * to inherit from, or on new records without a parent. |
||
| 380 | * |
||
| 381 | * @param Member $member |
||
| 382 | * @return boolean |
||
| 383 | */ |
||
| 384 | public function canEditPages($member = null) |
||
| 385 | { |
||
| 386 | if (!$member) { |
||
| 387 | $member = Security::getCurrentUser(); |
||
| 388 | } |
||
| 389 | |||
| 390 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
| 391 | return true; |
||
| 392 | } |
||
| 393 | |||
| 394 | $extended = $this->extendedCan('canEditPages', $member); |
||
| 395 | if ($extended !== null) { |
||
| 396 | return $extended; |
||
| 397 | } |
||
| 398 | |||
| 399 | // check for any logged-in users with CMS access |
||
| 400 | if ($this->CanEditType === 'LoggedInUsers' |
||
| 401 | && Permission::checkMember($member, $this->config()->get('required_permission')) |
||
| 402 | ) { |
||
| 403 | return true; |
||
| 404 | } |
||
| 405 | |||
| 406 | // check for specific groups |
||
| 407 | if ($this->CanEditType === 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) { |
||
| 408 | return true; |
||
| 409 | } |
||
| 410 | |||
| 411 | return false; |
||
| 412 | } |
||
| 413 | |||
| 414 | public function canEdit($member = null) |
||
| 415 | { |
||
| 416 | if (!$member) { |
||
| 417 | $member = Security::getCurrentUser(); |
||
| 418 | } |
||
| 419 | |||
| 420 | $extended = $this->extendedCan('canEdit', $member); |
||
| 421 | if ($extended !== null) { |
||
| 422 | return $extended; |
||
| 423 | } |
||
| 424 | |||
| 425 | return Permission::checkMember($member, "EDIT_SITECONFIG"); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | public function providePermissions() |
||
| 432 | { |
||
| 433 | return [ |
||
| 434 | 'EDIT_SITECONFIG' => [ |
||
| 435 | 'name' => _t(self::class . '.EDIT_PERMISSION', 'Manage site configuration'), |
||
| 436 | 'category' => _t( |
||
| 437 | 'SilverStripe\\Security\\Permission.PERMISSIONS_CATEGORY', |
||
| 438 | 'Roles and access permissions' |
||
| 439 | ), |
||
| 440 | 'help' => _t( |
||
| 441 | self::class . '.EDIT_PERMISSION_HELP', |
||
| 442 | 'Ability to edit global access settings/top-level page permissions.' |
||
| 443 | ), |
||
| 444 | 'sort' => 400 |
||
| 445 | ] |
||
| 446 | ]; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Can a user create pages in the root of this site? |
||
| 451 | * |
||
| 452 | * @param Member $member |
||
| 453 | * @return boolean |
||
| 454 | */ |
||
| 455 | public function canCreateTopLevel($member = null) |
||
| 456 | { |
||
| 457 | if (!$member) { |
||
| 458 | $member = Security::getCurrentUser(); |
||
| 459 | } |
||
| 460 | |||
| 461 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
| 462 | return true; |
||
| 463 | } |
||
| 464 | |||
| 465 | $extended = $this->extendedCan('canCreateTopLevel', $member); |
||
| 466 | if ($extended !== null) { |
||
| 467 | return $extended; |
||
| 468 | } |
||
| 469 | |||
| 470 | // check for any logged-in users with CMS permission |
||
| 471 | if ($this->CanCreateTopLevelType === 'LoggedInUsers' |
||
| 472 | && Permission::checkMember($member, $this->config()->get('required_permission')) |
||
| 473 | ) { |
||
| 474 | return true; |
||
| 475 | } |
||
| 476 | |||
| 477 | // check for specific groups |
||
| 478 | if ($this->CanCreateTopLevelType === 'OnlyTheseUsers' |
||
| 479 | && $member |
||
| 480 | && $member->inGroups($this->CreateTopLevelGroups()) |
||
| 481 | ) { |
||
| 482 | return true; |
||
| 483 | } |
||
| 484 | |||
| 485 | return false; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Add $SiteConfig to all SSViewers |
||
| 490 | */ |
||
| 491 | public static function get_template_global_variables() |
||
| 495 | ]; |
||
| 496 | } |
||
| 497 | } |
||
| 498 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths