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 |
||
| 52 | class ConfigService { |
||
| 53 | |||
| 54 | |||
| 55 | use TStringTools; |
||
| 56 | use TArrayTools; |
||
| 57 | |||
| 58 | |||
| 59 | const FRONTAL_CLOUD_ID = 'frontal_cloud_id'; |
||
| 60 | const FRONTAL_CLOUD_SCHEME = 'frontal_cloud_scheme'; |
||
| 61 | const INTERNAL_CLOUD_ID = 'internal_cloud_id'; |
||
| 62 | const INTERNAL_CLOUD_SCHEME = 'internal_cloud_scheme'; |
||
| 63 | const LOOPBACK_CLOUD_ID = 'loopback_cloud_id'; |
||
| 64 | const LOOPBACK_CLOUD_SCHEME = 'loopback_cloud_scheme'; |
||
| 65 | const IFACE0_CLOUD_ID = 'iface0_cloud_id'; |
||
| 66 | const IFACE0_CLOUD_SCHEME = 'iface0_cloud_scheme'; |
||
| 67 | const IFACE1_CLOUD_ID = 'iface1_cloud_id'; |
||
| 68 | const IFACE1_CLOUD_SCHEME = 'iface1_cloud_scheme'; |
||
| 69 | const IFACE2_CLOUD_ID = 'iface2_cloud_id'; |
||
| 70 | const IFACE2_CLOUD_SCHEME = 'iface2_cloud_scheme'; |
||
| 71 | const IFACE3_CLOUD_ID = 'iface3_cloud_id'; |
||
| 72 | const IFACE3_CLOUD_SCHEME = 'iface3_cloud_scheme'; |
||
| 73 | const IFACE4_CLOUD_ID = 'iface4_cloud_id'; |
||
| 74 | const IFACE4_CLOUD_SCHEME = 'iface4_cloud_scheme'; |
||
| 75 | |||
| 76 | const SELF_SIGNED_CERT = 'self_signed_cert'; |
||
| 77 | const MEMBERS_LIMIT = 'members_limit'; |
||
| 78 | const ACTIVITY_ON_NEW_CIRCLE = 'creation_activity'; |
||
| 79 | const MIGRATION_22 = 'migration_22'; |
||
| 80 | |||
| 81 | const LOOPBACK_TMP_ID = 'loopback_tmp_id'; |
||
| 82 | const LOOPBACK_TMP_SCHEME = 'loopback_tmp_scheme'; |
||
| 83 | |||
| 84 | const GS_MODE = 'mode'; |
||
| 85 | const GS_KEY = 'key'; |
||
| 86 | |||
| 87 | const GS_LOOKUP_INSTANCES = '/instances'; |
||
| 88 | const GS_LOOKUP_USERS = '/users'; |
||
| 89 | |||
| 90 | |||
| 91 | // deprecated -- removing in NC25 |
||
| 92 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
| 93 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; // only UserType=1 |
||
| 94 | const CIRCLES_SEARCH_FROM_COLLABORATOR = 'search_from_collaborator'; |
||
| 95 | |||
| 96 | const FORCE_NC_BASE = 'force_nc_base'; |
||
| 97 | const TEST_NC_BASE = 'test_nc_base'; |
||
| 98 | |||
| 99 | |||
| 100 | private $defaults = [ |
||
| 101 | self::FRONTAL_CLOUD_ID => '', |
||
| 102 | self::FRONTAL_CLOUD_SCHEME => 'https', |
||
| 103 | self::INTERNAL_CLOUD_ID => '', |
||
| 104 | self::INTERNAL_CLOUD_SCHEME => 'https', |
||
| 105 | self::LOOPBACK_CLOUD_ID => '', |
||
| 106 | self::LOOPBACK_CLOUD_SCHEME => 'https', |
||
| 107 | self::LOOPBACK_TMP_ID => '', |
||
| 108 | self::LOOPBACK_TMP_SCHEME => '', |
||
| 109 | self::IFACE0_CLOUD_ID => '', |
||
| 110 | self::IFACE0_CLOUD_SCHEME => 'https', |
||
| 111 | self::IFACE1_CLOUD_ID => '', |
||
| 112 | self::IFACE1_CLOUD_SCHEME => 'https', |
||
| 113 | self::IFACE2_CLOUD_ID => '', |
||
| 114 | self::IFACE2_CLOUD_SCHEME => 'https', |
||
| 115 | self::IFACE3_CLOUD_ID => '', |
||
| 116 | self::IFACE3_CLOUD_SCHEME => 'https', |
||
| 117 | self::IFACE4_CLOUD_ID => '', |
||
| 118 | self::IFACE4_CLOUD_SCHEME => 'https', |
||
| 119 | |||
| 120 | self::SELF_SIGNED_CERT => '0', |
||
| 121 | self::MEMBERS_LIMIT => '50', |
||
| 122 | self::ACTIVITY_ON_NEW_CIRCLE => '1', |
||
| 123 | self::MIGRATION_22 => '0', |
||
| 124 | |||
| 125 | self::FORCE_NC_BASE => '', |
||
| 126 | self::TEST_NC_BASE => '', |
||
| 127 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
| 128 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
| 129 | self::CIRCLES_SEARCH_FROM_COLLABORATOR => '0', |
||
| 130 | ]; |
||
| 131 | |||
| 132 | |||
| 133 | /** @var IConfig */ |
||
| 134 | private $config; |
||
| 135 | |||
| 136 | /** @var IURLGenerator */ |
||
| 137 | private $urlGenerator; |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * ConfigService constructor. |
||
| 142 | * |
||
| 143 | * @param IConfig $config |
||
| 144 | * @param IURLGenerator $urlGenerator |
||
| 145 | */ |
||
| 146 | public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Get a value by key |
||
| 154 | * |
||
| 155 | * @param string $key |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function getAppValue(string $key): string { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $key |
||
| 173 | * |
||
| 174 | * @return int |
||
| 175 | */ |
||
| 176 | public function getAppValueInt(string $key): int { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $key |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function getAppValueBool(string $key): bool { |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Set a value by key |
||
| 192 | * |
||
| 193 | * @param string $key |
||
| 194 | * @param string $value |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function setAppValue(string $key, string $value): void { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * |
||
| 205 | */ |
||
| 206 | public function unsetAppConfig(): void { |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * Get available hosts |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function getAvailableHosts(): array { |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Get a user value by key and user |
||
| 223 | * |
||
| 224 | * |
||
| 225 | * @param string $userId |
||
| 226 | * @param string $key |
||
| 227 | * |
||
| 228 | * @param string $default |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @return bool |
||
| 239 | * @deprecated |
||
| 240 | */ |
||
| 241 | public function isContactsBackend(): bool { |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * @return int |
||
| 249 | * @deprecated |
||
| 250 | */ |
||
| 251 | public function contactsBackendType(): int { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | * @deprecated |
||
| 259 | * should the password for a mail share be send to the recipient |
||
| 260 | * |
||
| 261 | */ |
||
| 262 | public function sendPasswordByMail() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param DeprecatedCircle $circle |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | * @deprecated |
||
| 275 | * do we require a share by mail to be password protected |
||
| 276 | * |
||
| 277 | */ |
||
| 278 | public function enforcePasswordProtection(DeprecatedCircle $circle) { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * // TODO: fetch data from somewhere else than hard coded... |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | public function getSettings(): array { |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | public function isGSAvailable(): bool { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @return string |
||
| 319 | * @throws GSStatusException |
||
| 320 | */ |
||
| 321 | public function getGSLookup(): string { |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function getGSSMockup(): array { |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $type |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function getGSInfo(string $type): string { |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * @return array |
||
| 364 | * @throws GSStatusException |
||
| 365 | */ |
||
| 366 | public function getGSData(): array { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | public function getTrustedDomains(): array { |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function getLoopbackInstance(): string { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * returns loopback address based on getLoopbackInstance and LOOPBACK_CLOUD_SCHEME |
||
| 433 | * should be used to async process |
||
| 434 | * |
||
| 435 | * @param string $route |
||
| 436 | * @param array $args |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getLoopbackPath(string $route = '', array $args = []): string { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * - must be configured using INTERNAL_CLOUD_ID |
||
| 458 | * - returns host+port, does not specify any protocol |
||
| 459 | * - used mainly to assign instance and source to a request to local GlobalScale |
||
| 460 | * - important only in GlobalScale environment |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | * |
||
| 464 | */ |
||
| 465 | public function getInternalInstance(): string { |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * - must be configured using FRONTAL_CLOUD_ID |
||
| 472 | * - returns host+port, does not specify any protocol |
||
| 473 | * - used mainly to assign instance and source to a request |
||
| 474 | * - important only in remote environment |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public function getFrontalInstance(): string { |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * @param int $iface |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function getIfaceInstance(int $iface): string { |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $instance |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | public function isLocalInstance(string $instance): bool { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $instance |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function displayInstance(string $instance): string { |
||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * - Create route using getLoopbackAddress() |
||
| 554 | * - perfect for loopback request. |
||
| 555 | * |
||
| 556 | * @param NC22Request $request |
||
| 557 | * @param string $route |
||
| 558 | * @param array $args |
||
| 559 | */ |
||
| 560 | public function configureLoopbackRequest( |
||
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * @param NC22Request $request |
||
| 572 | */ |
||
| 573 | public function configureRequest(NC22Request $request): void { |
||
| 581 | |||
| 582 | } |
||
| 583 | |||
| 584 |
This method has been deprecated.