Complex classes like ConfigService 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 ConfigService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class ConfigService { |
||
| 41 | |||
| 42 | |||
| 43 | use TStringTools; |
||
| 44 | |||
| 45 | |||
| 46 | const CIRCLES_ALLOW_CIRCLES = 'allow_circles'; |
||
| 47 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
| 48 | const CIRCLES_STILL_FRONTEND = 'still_frontend'; |
||
| 49 | const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams'; |
||
| 50 | const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated'; |
||
| 51 | const CIRCLES_GS_ENABLED = 'gs_enabled'; |
||
| 52 | const CIRCLES_MEMBERS_LIMIT = 'members_limit'; |
||
| 53 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; |
||
| 54 | const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups'; |
||
| 55 | const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links'; |
||
| 56 | const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl'; |
||
| 57 | const CIRCLES_SELF_SIGNED = 'self_signed_cert'; |
||
| 58 | const CIRCLES_LOCAL_GSKEY = 'local_gskey'; |
||
| 59 | const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity'; |
||
| 60 | const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles'; |
||
| 61 | const CIRCLES_SEARCH_FROM_COLLABORATOR = 'search_from_collaborator'; |
||
| 62 | const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock'; |
||
| 63 | const CIRCLES_TEST_ASYNC_INIT = 'test_async_init'; |
||
| 64 | const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand'; |
||
| 65 | const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count'; |
||
| 66 | |||
| 67 | const FRONTAL_CLOUD_ID = 'frontal_cloud_id'; |
||
| 68 | const FRONTAL_CLOUD_SCHEME = 'frontal_cloud_scheme'; |
||
| 69 | const INTERNAL_CLOUD_ID = 'internal_cloud_id'; |
||
| 70 | const INTERNAL_CLOUD_SCHEME = 'internal_cloud_scheme'; |
||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | const FORCE_NC_BASE = 'force_nc_base'; |
||
| 75 | const TEST_NC_BASE = 'test_nc_base'; |
||
| 76 | |||
| 77 | const GS_ENABLED = 'enabled'; |
||
| 78 | const GS_MODE = 'mode'; |
||
| 79 | const GS_KEY = 'key'; |
||
| 80 | const GS_LOOKUP = 'lookup'; |
||
| 81 | const GS_MOCKUP = 'mockup'; |
||
| 82 | |||
| 83 | const GS_LOOKUP_INSTANCES = '/instances'; |
||
| 84 | const GS_LOOKUP_USERS = '/users'; |
||
| 85 | |||
| 86 | |||
| 87 | private $defaults = [ |
||
| 88 | self::CIRCLES_ALLOW_CIRCLES => DeprecatedCircle::CIRCLES_ALL, |
||
| 89 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
| 90 | self::CIRCLES_STILL_FRONTEND => '0', |
||
| 91 | self::CIRCLES_TEST_ASYNC_INIT => '0', |
||
| 92 | self::CIRCLES_SWAP_TO_TEAMS => '0', |
||
| 93 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
| 94 | self::CIRCLES_MEMBERS_LIMIT => '50', |
||
| 95 | self::CIRCLES_ALLOW_LINKED_GROUPS => '0', |
||
| 96 | self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0', |
||
| 97 | self::CIRCLES_GS_ENABLED => '0', |
||
| 98 | self::CIRCLES_LOCAL_GSKEY => '', |
||
| 99 | self::CIRCLES_ALLOW_NON_SSL_LINKS => '0', |
||
| 100 | self::CIRCLES_NON_SSL_LOCAL => '0', |
||
| 101 | self::CIRCLES_SELF_SIGNED => '0', |
||
| 102 | self::FRONTAL_CLOUD_ID => '', |
||
| 103 | self::FRONTAL_CLOUD_SCHEME => 'https', |
||
| 104 | self::INTERNAL_CLOUD_ID => '', |
||
| 105 | self::INTERNAL_CLOUD_SCHEME => 'https', |
||
| 106 | self::FORCE_NC_BASE => '', |
||
| 107 | self::TEST_NC_BASE => '', |
||
| 108 | self::CIRCLES_ACTIVITY_ON_CREATION => '1', |
||
| 109 | self::CIRCLES_SKIP_INVITATION_STEP => '0', |
||
| 110 | self::CIRCLES_SEARCH_FROM_COLLABORATOR => '0' |
||
| 111 | ]; |
||
| 112 | |||
| 113 | /** @var string */ |
||
| 114 | private $appName; |
||
| 115 | |||
| 116 | /** @var IConfig */ |
||
| 117 | private $config; |
||
| 118 | |||
| 119 | /** @var string */ |
||
| 120 | private $userId; |
||
| 121 | |||
| 122 | /** @var IRequest */ |
||
| 123 | private $request; |
||
| 124 | |||
| 125 | /** @var IURLGenerator */ |
||
| 126 | private $urlGenerator; |
||
| 127 | |||
| 128 | /** @var MiscService */ |
||
| 129 | private $miscService; |
||
| 130 | |||
| 131 | /** @var int */ |
||
| 132 | private $allowedCircle = -1; |
||
| 133 | |||
| 134 | /** @var int */ |
||
| 135 | private $allowedLinkedGroups = -1; |
||
| 136 | |||
| 137 | /** @var int */ |
||
| 138 | private $allowedFederatedCircles = -1; |
||
| 139 | |||
| 140 | /** @var int */ |
||
| 141 | private $allowedNonSSLLinks = -1; |
||
| 142 | |||
| 143 | /** @var int */ |
||
| 144 | private $localNonSSL = -1; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * ConfigService constructor. |
||
| 148 | * |
||
| 149 | * @param string $appName |
||
| 150 | * @param IConfig $config |
||
| 151 | * @param IRequest $request |
||
| 152 | * @param string $userId |
||
| 153 | * @param IURLGenerator $urlGenerator |
||
| 154 | * @param MiscService $miscService |
||
| 155 | */ |
||
| 156 | public function __construct( |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | * @deprecated |
||
| 172 | */ |
||
| 173 | public function getLocalAddress() { |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * returns if this type of circle is allowed by the current configuration. |
||
| 181 | * |
||
| 182 | * @param $type |
||
| 183 | * |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | public function isCircleAllowed($type) { |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @return bool |
||
| 197 | * @throws GSStatusException |
||
| 198 | */ |
||
| 199 | public function isLinkedGroupsAllowed() { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function isFederatedCirclesAllowed() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function isInvitationSkipped() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function isLocalNonSSL() { |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function isNonSSLLinksAllowed() { |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $remote |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function generateRemoteHost($remote) { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Get a value by key |
||
| 273 | * |
||
| 274 | * @param string $key |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getCoreValue($key) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get a value by key |
||
| 286 | * |
||
| 287 | * @param string $key |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getSystemValue($key) { |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * Get available hosts |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function getAvailableHosts(): array { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Get a value by key |
||
| 310 | * |
||
| 311 | * @param string $key |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getAppValue($key) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set a value by key |
||
| 327 | * |
||
| 328 | * @param string $key |
||
| 329 | * @param string $value |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | public function setAppValue($key, $value) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * remove a key |
||
| 339 | * |
||
| 340 | * @param string $key |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function deleteAppValue($key): string { |
||
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * |
||
| 351 | */ |
||
| 352 | public function unsetAppConfig() { |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * Get a user value by key |
||
| 359 | * |
||
| 360 | * @param string $key |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | public function getUserValue($key) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Set a user value by key |
||
| 370 | * |
||
| 371 | * @param string $key |
||
| 372 | * @param string $value |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | * @throws PreConditionNotMetException |
||
| 376 | */ |
||
| 377 | public function setUserValue($key, $value) { |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Get a user value by key and user |
||
| 384 | * |
||
| 385 | * @param string $userId |
||
| 386 | * @param string $key |
||
| 387 | * |
||
| 388 | * @param string $default |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Get a user value by key and user |
||
| 399 | * |
||
| 400 | * @param string $userId |
||
| 401 | * @param string $key |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function getValueForUser($userId, $key) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Set a user value by key |
||
| 411 | * |
||
| 412 | * @param string $userId |
||
| 413 | * @param string $key |
||
| 414 | * @param string $value |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | * @throws PreConditionNotMetException |
||
| 418 | */ |
||
| 419 | public function setValueForUser($userId, $key, $value) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * return the cloud version. |
||
| 425 | * if $complete is true, return a string x.y.z |
||
| 426 | * |
||
| 427 | * @param boolean $complete |
||
| 428 | * |
||
| 429 | * @return string|integer |
||
| 430 | */ |
||
| 431 | public function getCloudVersion($complete = false) { |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | public function isAccountOnly() { |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | public function isContactsBackend(): bool { |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * @return int |
||
| 461 | */ |
||
| 462 | public function contactsBackendType(): int { |
||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * @return bool |
||
| 469 | */ |
||
| 470 | public function stillFrontEnd(): bool { |
||
| 481 | |||
| 482 | |||
| 483 | /** |
||
| 484 | * should the password for a mail share be send to the recipient |
||
| 485 | * |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | public function sendPasswordByMail() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * do we require a share by mail to be password protected |
||
| 498 | * |
||
| 499 | * @param DeprecatedCircle $circle |
||
| 500 | * |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | public function enforcePasswordProtection(DeprecatedCircle $circle) { |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $type |
||
| 518 | * |
||
| 519 | * @return array|bool|mixed |
||
| 520 | * @throws GSStatusException |
||
| 521 | */ |
||
| 522 | public function getGSStatus(string $type = '') { |
||
| 566 | |||
| 567 | |||
| 568 | /** |
||
| 569 | * @return array |
||
| 570 | */ |
||
| 571 | public function getTrustedDomains(): array { |
||
| 574 | |||
| 575 | |||
| 576 | /** |
||
| 577 | * - returns host+port, does not specify any protocol |
||
| 578 | * - can be forced using FRONTAL_CLOUD_ID |
||
| 579 | * - use 'overwrite.cli.url' |
||
| 580 | * - can use the first entry from trusted_domains if FRONTAL_CLOUD_ID = 'use-trusted-domain' |
||
| 581 | * - used mainly to assign instance and source to a request |
||
| 582 | * - important only in remote environment; can be totally random in a jailed environment |
||
| 583 | * |
||
| 584 | * @return string |
||
| 585 | */ |
||
| 586 | public function getFrontalInstance(): string { |
||
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * returns address based on FRONTAL_CLOUD_ID, FRONTAL_CLOUD_SCHEME and a routeName |
||
| 624 | * perfect for urlId in ActivityPub env. |
||
| 625 | * |
||
| 626 | * @param string $route |
||
| 627 | * @param array $args |
||
| 628 | * |
||
| 629 | * @return string |
||
| 630 | */ |
||
| 631 | public function getFrontalPath(string $route = 'circles.Remote.appService', array $args = []): string { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @param string $instance |
||
| 643 | * |
||
| 644 | * @return bool |
||
| 645 | */ |
||
| 646 | public function isLocalInstance(string $instance): bool { |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * @param NC21Request $request |
||
| 661 | * @param string $routeName |
||
| 662 | * @param array $args |
||
| 663 | */ |
||
| 664 | public function configureRequest(NC21Request $request, string $routeName = '', array $args = []) { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * - Create route using overwrite.cli.url. |
||
| 679 | * - can be forced using FORCE_NC_BASE or TEST_BC_BASE (temporary) |
||
| 680 | * - can also be overwritten in config/config.php: 'circles.force_nc_base' |
||
| 681 | * - perfect for loopback request. |
||
| 682 | * |
||
| 683 | * @param NC21Request $request |
||
| 684 | * @param string $routeName |
||
| 685 | * @param array $args |
||
| 686 | * |
||
| 687 | * @return string |
||
| 688 | */ |
||
| 689 | private function configureRequestAddress(NC21Request $request, string $routeName, array $args = []) { |
||
| 703 | |||
| 704 | |||
| 705 | /** |
||
| 706 | * - return force_nc_base from config/config.php, then from FORCE_NC_BASE. |
||
| 707 | * |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | private function getForcedNcBase(): string { |
||
| 722 | |||
| 723 | |||
| 724 | /** |
||
| 725 | * sometimes, linkToRoute will include the base path to the nextcloud which will be duplicate with ncBase |
||
| 726 | * |
||
| 727 | * @param string $ncBase |
||
| 728 | * @param string $routeName |
||
| 729 | * @param array $args |
||
| 730 | * |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | private function cleanLinkToRoute(string $ncBase, string $routeName, array $args): string { |
||
| 743 | |||
| 744 | } |
||
| 745 | |||
| 746 |