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 | use TArrayTools; |
||
45 | |||
46 | |||
47 | const FRONTAL_CLOUD_ID = 'frontal_cloud_id'; |
||
48 | const FRONTAL_CLOUD_SCHEME = 'frontal_cloud_scheme'; |
||
49 | const INTERNAL_CLOUD_ID = 'internal_cloud_id'; |
||
50 | const INTERNAL_CLOUD_SCHEME = 'internal_cloud_scheme'; |
||
51 | const LOOPBACK_CLOUD_ID = 'loopback_cloud_id'; |
||
52 | const LOOPBACK_CLOUD_SCHEME = 'loopback_cloud_scheme'; |
||
53 | const SELF_SIGNED_CERT = 'self_signed_cert'; |
||
54 | const MEMBERS_LIMIT = 'members_limit'; |
||
55 | const ACTIVITY_ON_NEW_CIRCLE = 'creation_activity'; |
||
56 | const MIGRATION_22 = 'migration_22'; |
||
57 | |||
58 | // deprecated |
||
59 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
60 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; // only UserType=1 |
||
61 | const CIRCLES_SEARCH_FROM_COLLABORATOR = 'search_from_collaborator'; |
||
62 | |||
63 | |||
64 | const FORCE_NC_BASE = 'force_nc_base'; |
||
65 | const TEST_NC_BASE = 'test_nc_base'; |
||
66 | |||
67 | const GS_MODE = 'mode'; |
||
68 | const GS_KEY = 'key'; |
||
69 | |||
70 | const GS_LOOKUP_INSTANCES = '/instances'; |
||
71 | const GS_LOOKUP_USERS = '/users'; |
||
72 | |||
73 | |||
74 | private $defaults = [ |
||
75 | self::FRONTAL_CLOUD_ID => '', |
||
76 | self::FRONTAL_CLOUD_SCHEME => 'https', |
||
77 | self::INTERNAL_CLOUD_ID => '', |
||
78 | self::INTERNAL_CLOUD_SCHEME => 'https', |
||
79 | self::LOOPBACK_CLOUD_ID => '', |
||
80 | self::LOOPBACK_CLOUD_SCHEME => 'https', |
||
81 | self::SELF_SIGNED_CERT => '0', |
||
82 | self::MEMBERS_LIMIT => '50', |
||
83 | self::ACTIVITY_ON_NEW_CIRCLE => '1', |
||
84 | self::MIGRATION_22 => '0', |
||
85 | |||
86 | self::FORCE_NC_BASE => '', |
||
87 | self::TEST_NC_BASE => '', |
||
88 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
89 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
90 | self::CIRCLES_SEARCH_FROM_COLLABORATOR => '0', |
||
91 | ]; |
||
92 | |||
93 | |||
94 | /** @var IConfig */ |
||
95 | private $config; |
||
96 | |||
97 | /** @var IURLGenerator */ |
||
98 | private $urlGenerator; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * ConfigService constructor. |
||
103 | * |
||
104 | * @param IConfig $config |
||
105 | * @param IURLGenerator $urlGenerator |
||
106 | */ |
||
107 | public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Get a value by key |
||
115 | * |
||
116 | * @param string $key |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getAppValue(string $key): string { |
||
131 | |||
132 | /** |
||
133 | * @param string $key |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | public function getAppValueInt(string $key): int { |
||
140 | |||
141 | /** |
||
142 | * @param string $key |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function getAppValueBool(string $key): bool { |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Set a value by key |
||
153 | * |
||
154 | * @param string $key |
||
155 | * @param string $value |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | public function setAppValue(string $key, string $value): void { |
||
162 | |||
163 | |||
164 | /** |
||
165 | * |
||
166 | */ |
||
167 | public function unsetAppConfig(): void { |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Get available hosts |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | public function getAvailableHosts(): array { |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Get a user value by key and user |
||
184 | * |
||
185 | * |
||
186 | * @param string $userId |
||
187 | * @param string $key |
||
188 | * |
||
189 | * @param string $default |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
196 | |||
197 | |||
198 | /** |
||
199 | * @return bool |
||
200 | * @deprecated |
||
201 | */ |
||
202 | public function isContactsBackend(): bool { |
||
206 | |||
207 | |||
208 | /** |
||
209 | * @return int |
||
210 | * @deprecated |
||
211 | */ |
||
212 | public function contactsBackendType(): int { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @return bool |
||
219 | * @deprecated |
||
220 | * should the password for a mail share be send to the recipient |
||
221 | * |
||
222 | */ |
||
223 | public function sendPasswordByMail() { |
||
230 | |||
231 | /** |
||
232 | * @param DeprecatedCircle $circle |
||
233 | * |
||
234 | * @return bool |
||
235 | * @deprecated |
||
236 | * do we require a share by mail to be password protected |
||
237 | * |
||
238 | */ |
||
239 | public function enforcePasswordProtection(DeprecatedCircle $circle) { |
||
250 | |||
251 | |||
252 | /** |
||
253 | * // TODO: fetch data from somewhere else than hard coded... |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | public function getSettings(): array { |
||
264 | |||
265 | |||
266 | /** |
||
267 | * @return bool |
||
268 | */ |
||
269 | public function isGSAvailable(): bool { |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @return string |
||
280 | * @throws GSStatusException |
||
281 | */ |
||
282 | public function getGSLookup(): string { |
||
291 | |||
292 | |||
293 | /** |
||
294 | * @return array |
||
295 | */ |
||
296 | public function getGSSMockup(): array { |
||
299 | |||
300 | |||
301 | /** |
||
302 | * @param string $type |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public function getGSInfo(string $type): string { |
||
321 | |||
322 | |||
323 | /** |
||
324 | * @return array |
||
325 | * @throws GSStatusException |
||
326 | */ |
||
327 | public function getGSData(): array { |
||
336 | |||
337 | |||
338 | /** |
||
339 | * @return array |
||
340 | */ |
||
341 | public function getTrustedDomains(): array { |
||
344 | |||
345 | |||
346 | /** |
||
347 | * - returns host+port, does not specify any protocol |
||
348 | * - can be forced using FRONTAL_CLOUD_ID |
||
349 | * - use 'overwrite.cli.url' |
||
350 | * - can use the first entry from trusted_domains if FRONTAL_CLOUD_ID = 'use-trusted-domain' |
||
351 | * - used mainly to assign instance and source to a request |
||
352 | * - important only in remote environment; can be totally random in a jailed environment |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function getFrontalInstance(): string { |
||
392 | |||
393 | |||
394 | /** |
||
395 | * returns address based on FRONTAL_CLOUD_ID, FRONTAL_CLOUD_SCHEME and a routeName |
||
396 | * perfect for urlId in ActivityPub env. |
||
397 | * |
||
398 | * @param string $route |
||
399 | * @param array $args |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | public function getFrontalPath(string $route = 'circles.Remote.appService', array $args = []): string { |
||
412 | |||
413 | /** |
||
414 | * @param string $instance |
||
415 | * |
||
416 | * @return bool |
||
417 | */ |
||
418 | public function isLocalInstance(string $instance): bool { |
||
429 | |||
430 | |||
431 | /** |
||
432 | * @param NC22Request $request |
||
433 | * @param string $routeName |
||
434 | * @param array $args |
||
435 | */ |
||
436 | public function configureRequest(NC22Request $request, string $routeName = '', array $args = []): void { |
||
449 | |||
450 | /** |
||
451 | * - Create route using overwrite.cli.url. |
||
452 | * - can be forced using FORCE_NC_BASE or TEST_BC_BASE (temporary) |
||
453 | * - can also be overwritten in config/config.php: 'circles.force_nc_base' |
||
454 | * - perfect for loopback request. |
||
455 | * |
||
456 | * @param NC22Request $request |
||
457 | * @param string $routeName |
||
458 | * @param array $args |
||
459 | */ |
||
460 | private function configureRequestAddress( |
||
478 | |||
479 | |||
480 | /** |
||
481 | * - return force_nc_base from config/config.php, then from FORCE_NC_BASE. |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | private function getForcedNcBase(): string { |
||
497 | |||
498 | |||
499 | /** |
||
500 | * sometimes, linkToRoute will include the base path to the nextcloud which will be duplicate with ncBase |
||
501 | * |
||
502 | * @param string $ncBase |
||
503 | * @param string $routeName |
||
504 | * @param array $args |
||
505 | * |
||
506 | * @return string |
||
507 | */ |
||
508 | private function cleanLinkToRoute(string $ncBase, string $routeName, array $args): string { |
||
518 | |||
519 | } |
||
520 | |||
521 |
This method has been deprecated.