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 |
||
| 41 | class ConfigService { |
||
| 42 | |||
| 43 | |||
| 44 | use TStringTools; |
||
| 45 | use TArrayTools; |
||
| 46 | |||
| 47 | |||
| 48 | const FRONTAL_CLOUD_ID = 'frontal_cloud_id'; |
||
| 49 | const FRONTAL_CLOUD_SCHEME = 'frontal_cloud_scheme'; |
||
| 50 | const INTERNAL_CLOUD_ID = 'internal_cloud_id'; |
||
| 51 | const INTERNAL_CLOUD_SCHEME = 'internal_cloud_scheme'; |
||
| 52 | const LOOPBACK_CLOUD_ID = 'loopback_cloud_id'; |
||
| 53 | const LOOPBACK_CLOUD_SCHEME = 'loopback_cloud_scheme'; |
||
| 54 | const CHECK_FRONTAL_USING = 'check_frontal_using'; |
||
| 55 | const CHECK_INTERNAL_USING = 'check_internal_using'; |
||
| 56 | const SELF_SIGNED_CERT = 'self_signed_cert'; |
||
| 57 | const MEMBERS_LIMIT = 'members_limit'; |
||
| 58 | const ACTIVITY_ON_NEW_CIRCLE = 'creation_activity'; |
||
| 59 | const MIGRATION_22 = 'migration_22'; |
||
| 60 | |||
| 61 | const LOOPBACK_TMP_ID = 'loopback_tmp_id'; |
||
| 62 | const LOOPBACK_TMP_SCHEME = 'loopback_tmp_scheme'; |
||
| 63 | |||
| 64 | const GS_MODE = 'mode'; |
||
| 65 | const GS_KEY = 'key'; |
||
| 66 | |||
| 67 | const GS_LOOKUP_INSTANCES = '/instances'; |
||
| 68 | const GS_LOOKUP_USERS = '/users'; |
||
| 69 | |||
| 70 | |||
| 71 | // deprecated -- removing in NC25 |
||
| 72 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
| 73 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; // only UserType=1 |
||
| 74 | const CIRCLES_SEARCH_FROM_COLLABORATOR = 'search_from_collaborator'; |
||
| 75 | |||
| 76 | const FORCE_NC_BASE = 'force_nc_base'; |
||
| 77 | const TEST_NC_BASE = 'test_nc_base'; |
||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | private $defaults = [ |
||
| 82 | self::FRONTAL_CLOUD_ID => '', |
||
| 83 | self::FRONTAL_CLOUD_SCHEME => 'https', |
||
| 84 | self::INTERNAL_CLOUD_ID => '', |
||
| 85 | self::INTERNAL_CLOUD_SCHEME => 'https', |
||
| 86 | self::LOOPBACK_CLOUD_ID => '', |
||
| 87 | self::LOOPBACK_CLOUD_SCHEME => 'https', |
||
| 88 | self::CHECK_FRONTAL_USING => 'https://test.artificial-owl.com/', |
||
| 89 | self::CHECK_INTERNAL_USING => '', |
||
| 90 | self::LOOPBACK_TMP_ID => '', |
||
| 91 | self::LOOPBACK_TMP_SCHEME => '', |
||
| 92 | |||
| 93 | self::SELF_SIGNED_CERT => '0', |
||
| 94 | self::MEMBERS_LIMIT => '50', |
||
| 95 | self::ACTIVITY_ON_NEW_CIRCLE => '1', |
||
| 96 | self::MIGRATION_22 => '0', |
||
| 97 | |||
| 98 | self::FORCE_NC_BASE => '', |
||
| 99 | self::TEST_NC_BASE => '', |
||
| 100 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
| 101 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
| 102 | self::CIRCLES_SEARCH_FROM_COLLABORATOR => '0', |
||
| 103 | ]; |
||
| 104 | |||
| 105 | |||
| 106 | /** @var IConfig */ |
||
| 107 | private $config; |
||
| 108 | |||
| 109 | /** @var IURLGenerator */ |
||
| 110 | private $urlGenerator; |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * ConfigService constructor. |
||
| 115 | * |
||
| 116 | * @param IConfig $config |
||
| 117 | * @param IURLGenerator $urlGenerator |
||
| 118 | */ |
||
| 119 | public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Get a value by key |
||
| 127 | * |
||
| 128 | * @param string $key |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getAppValue(string $key): string { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $key |
||
| 146 | * |
||
| 147 | * @return int |
||
| 148 | */ |
||
| 149 | public function getAppValueInt(string $key): int { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $key |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function getAppValueBool(string $key): bool { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Set a value by key |
||
| 165 | * |
||
| 166 | * @param string $key |
||
| 167 | * @param string $value |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function setAppValue(string $key, string $value): void { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * |
||
| 178 | */ |
||
| 179 | public function unsetAppConfig(): void { |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Get available hosts |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function getAvailableHosts(): array { |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Get a user value by key and user |
||
| 196 | * |
||
| 197 | * |
||
| 198 | * @param string $userId |
||
| 199 | * @param string $key |
||
| 200 | * |
||
| 201 | * @param string $default |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @return bool |
||
| 212 | * @deprecated |
||
| 213 | */ |
||
| 214 | public function isContactsBackend(): bool { |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * @return int |
||
| 222 | * @deprecated |
||
| 223 | */ |
||
| 224 | public function contactsBackendType(): int { |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * @return bool |
||
| 231 | * @deprecated |
||
| 232 | * should the password for a mail share be send to the recipient |
||
| 233 | * |
||
| 234 | */ |
||
| 235 | public function sendPasswordByMail() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param DeprecatedCircle $circle |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | * @deprecated |
||
| 248 | * do we require a share by mail to be password protected |
||
| 249 | * |
||
| 250 | */ |
||
| 251 | public function enforcePasswordProtection(DeprecatedCircle $circle) { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * // TODO: fetch data from somewhere else than hard coded... |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function getSettings(): array { |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | public function isGSAvailable(): bool { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * @return string |
||
| 292 | * @throws GSStatusException |
||
| 293 | */ |
||
| 294 | public function getGSLookup(): string { |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | public function getGSSMockup(): array { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $type |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getGSInfo(string $type): string { |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @return array |
||
| 337 | * @throws GSStatusException |
||
| 338 | */ |
||
| 339 | public function getGSData(): array { |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function getTrustedDomains(): array { |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function getLoopbackInstance(): string { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * returns loopback address based on getLoopbackInstance and LOOPBACK_CLOUD_SCHEME |
||
| 402 | * should be used to async process |
||
| 403 | * |
||
| 404 | * @param string $route |
||
| 405 | * @param array $args |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function getLoopbackPath(string $route = '', array $args = []): string { |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * - must be configured using INTERNAL_CLOUD_ID |
||
| 427 | * - returns host+port, does not specify any protocol |
||
| 428 | * - used mainly to assign instance and source to a request to local GlobalScale |
||
| 429 | * - important only in GlobalScale environment |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function getInternalInstance(): string { |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * - must be configured using FRONTAL_CLOUD_ID |
||
| 440 | * - returns host+port, does not specify any protocol |
||
| 441 | * - used mainly to assign instance and source to a request |
||
| 442 | * - important only in remote environment |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function getFrontalInstance(): string { |
||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * returns address based on FRONTAL_CLOUD_ID, FRONTAL_CLOUD_SCHEME and a routeName |
||
| 463 | * perfect for urlId in ActivityPub env. |
||
| 464 | * |
||
| 465 | * @param bool $internal |
||
| 466 | * @param string $route |
||
| 467 | * @param array $args |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | * @throws RemoteInstanceException |
||
| 471 | */ |
||
| 472 | public function getInstancePath( |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param string $host |
||
| 494 | * @param string $route |
||
| 495 | * @param array $args |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | * @throws RemoteInstanceException |
||
| 499 | */ |
||
| 500 | public function getInstancePathBasedOnHost( |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $instance |
||
| 515 | * @param bool $internal |
||
| 516 | * |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | public function isLocalInstance(string $instance, bool $internal = false): bool { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param string $instance |
||
| 543 | * |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | public function displayInstance(string $instance): string { |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | public function getLocalInstance(): string { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | public function getValidLocalInstances(): array { |
||
| 587 | |||
| 588 | |||
| 589 | /** |
||
| 590 | * - Create route using getLoopbackAddress() |
||
| 591 | * - perfect for loopback request. |
||
| 592 | * |
||
| 593 | * @param NC22Request $request |
||
| 594 | * @param string $route |
||
| 595 | * @param array $args |
||
| 596 | */ |
||
| 597 | public function configureLoopbackRequest( |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * @param NC22Request $request |
||
| 609 | */ |
||
| 610 | public function configureRequest(NC22Request $request): void { |
||
| 618 | |||
| 619 | |||
| 620 | /** |
||
| 621 | * sometimes, linkToRoute will include the base path to the nextcloud which will be duplicate with ncBase |
||
| 622 | * |
||
| 623 | * @param string $ncBase |
||
| 624 | * @param string $routeName |
||
| 625 | * @param array $args |
||
| 626 | * |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | private function cleanLinkToRoute(string $ncBase, string $routeName, array $args): string { |
||
| 639 | |||
| 640 | } |
||
| 641 | |||
| 642 |
This method has been deprecated.