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 |
||
35 | class ConfigService { |
||
36 | |||
37 | const CIRCLES_ALLOW_CIRCLES = 'allow_circles'; |
||
38 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
39 | const CIRCLES_STILL_FRONTEND = 'still_frontend'; |
||
40 | const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams'; |
||
41 | const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated'; |
||
42 | const CIRCLES_MEMBERS_LIMIT = 'members_limit'; |
||
43 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; |
||
44 | const CIRCLES_ALLOW_ANY_GROUP_MEMBERS = 'allow_adding_any_group_members'; |
||
45 | const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups'; |
||
46 | const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links'; |
||
47 | const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl'; |
||
48 | const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity'; |
||
49 | const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles'; |
||
50 | |||
51 | const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock'; |
||
52 | const CIRCLES_TEST_ASYNC_INIT = 'test_async_init'; |
||
53 | const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand'; |
||
54 | const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count'; |
||
55 | |||
56 | private $defaults = [ |
||
57 | self::CIRCLES_ALLOW_CIRCLES => Circle::CIRCLES_ALL, |
||
58 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
59 | self::CIRCLES_STILL_FRONTEND => '0', |
||
60 | self::CIRCLES_TEST_ASYNC_INIT => '0', |
||
61 | self::CIRCLES_SWAP_TO_TEAMS => '0', |
||
62 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
63 | self::CIRCLES_MEMBERS_LIMIT => '50', |
||
64 | self::CIRCLES_ALLOW_LINKED_GROUPS => '0', |
||
65 | self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0', |
||
66 | self::CIRCLES_ALLOW_NON_SSL_LINKS => '0', |
||
67 | self::CIRCLES_NON_SSL_LOCAL => '0', |
||
68 | self::CIRCLES_ACTIVITY_ON_CREATION => '1', |
||
69 | self::CIRCLES_SKIP_INVITATION_STEP => '0' |
||
70 | self::CIRCLES_ALLOW_ANY_GROUP_MEMBERS => '1', |
||
|
|||
71 | ]; |
||
72 | |||
73 | /** @var string */ |
||
74 | private $appName; |
||
75 | |||
76 | /** @var IConfig */ |
||
77 | private $config; |
||
78 | |||
79 | /** @var string */ |
||
80 | private $userId; |
||
81 | |||
82 | /** @var IRequest */ |
||
83 | private $request; |
||
84 | |||
85 | /** @var MiscService */ |
||
86 | private $miscService; |
||
87 | |||
88 | /** @var int */ |
||
89 | private $allowedCircle = -1; |
||
90 | |||
91 | /** @var int */ |
||
92 | private $allowAddingAnyGroupMembers = -1; |
||
93 | |||
94 | /** @var int */ |
||
95 | private $allowedLinkedGroups = -1; |
||
96 | |||
97 | /** @var int */ |
||
98 | private $allowedFederatedCircles = -1; |
||
99 | |||
100 | /** @var int */ |
||
101 | private $allowedNonSSLLinks = -1; |
||
102 | |||
103 | /** @var int */ |
||
104 | private $localNonSSL = -1; |
||
105 | |||
106 | /** |
||
107 | * ConfigService constructor. |
||
108 | * |
||
109 | * @param string $appName |
||
110 | * @param IConfig $config |
||
111 | * @param IRequest $request |
||
112 | * @param string $userId |
||
113 | * @param MiscService $miscService |
||
114 | */ |
||
115 | public function __construct( |
||
116 | $appName, IConfig $config, IRequest $request, $userId, MiscService $miscService |
||
117 | ) { |
||
118 | $this->appName = $appName; |
||
119 | $this->config = $config; |
||
120 | $this->request = $request; |
||
121 | $this->userId = $userId; |
||
122 | $this->miscService = $miscService; |
||
123 | } |
||
124 | |||
125 | |||
126 | public function getLocalAddress() { |
||
127 | return (($this->isLocalNonSSL()) ? 'http://' : '') |
||
128 | . $this->request->getServerHost(); |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * returns if this type of circle is allowed by the current configuration. |
||
134 | * |
||
135 | * @param $type |
||
136 | * |
||
137 | * @return int |
||
138 | */ |
||
139 | public function isCircleAllowed($type) { |
||
140 | if ($this->allowedCircle === -1) { |
||
141 | $this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES); |
||
142 | } |
||
143 | |||
144 | return ((int)$type & (int)$this->allowedCircle); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * returns if the current user is allowed to add any group members. |
||
149 | * even if he isn't a member of these groups |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function isAddingAnyGroupMembersAllowed() { |
||
154 | if ($this->allowAddingAnyGroupMembers === -1) { |
||
155 | $this->allowAddingAnyGroupMembers = |
||
156 | (int)$this->getAppValue(self::CIRCLES_ALLOW_ANY_GROUP_MEMBERS); |
||
157 | } |
||
158 | |||
159 | return ($this->allowAddingAnyGroupMembers === 1); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function isLinkedGroupsAllowed() { |
||
166 | if ($this->allowedLinkedGroups === -1) { |
||
167 | $this->allowedLinkedGroups = |
||
168 | (int)$this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS); |
||
169 | } |
||
170 | |||
171 | return ($this->allowedLinkedGroups === 1); |
||
172 | } |
||
173 | |||
174 | |||
175 | /** |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function isFederatedCirclesAllowed() { |
||
179 | if ($this->allowedFederatedCircles === -1) { |
||
180 | $this->allowedFederatedCircles = |
||
181 | (int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED_CIRCLES); |
||
182 | } |
||
183 | |||
184 | return ($this->allowedFederatedCircles === 1); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function isInvitationSkipped() { |
||
191 | return (int)$this->getAppValue(self::CIRCLES_SKIP_INVITATION_STEP) === 1; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function isLocalNonSSL() { |
||
198 | if ($this->localNonSSL === -1) { |
||
199 | $this->localNonSSL = |
||
200 | (int)$this->getAppValue(self::CIRCLES_NON_SSL_LOCAL); |
||
201 | } |
||
202 | |||
203 | return ($this->localNonSSL === 1); |
||
204 | } |
||
205 | |||
206 | |||
207 | /** |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function isNonSSLLinksAllowed() { |
||
211 | if ($this->allowedNonSSLLinks === -1) { |
||
212 | $this->allowedNonSSLLinks = |
||
213 | (int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LINKS); |
||
214 | } |
||
215 | |||
216 | return ($this->allowedNonSSLLinks === 1); |
||
217 | } |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @param string $remote |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public function generateRemoteHost($remote) { |
||
226 | if ((!$this->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0) |
||
227 | && strpos($remote, 'https://') !== 0 |
||
228 | ) { |
||
229 | $remote = 'https://' . $remote; |
||
230 | } |
||
231 | |||
232 | return rtrim($remote, '/'); |
||
233 | } |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Get a value by key |
||
238 | * |
||
239 | * @param string $key |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getCoreValue($key) { |
||
244 | $defaultValue = null; |
||
245 | |||
246 | return $this->config->getAppValue('core', $key, $defaultValue); |
||
247 | } |
||
248 | |||
249 | |||
250 | /** |
||
251 | * Get available hosts |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | public function getAvailableHosts(): array { |
||
256 | return $this->config->getSystemValue('trusted_domains', []); |
||
257 | } |
||
258 | |||
259 | |||
260 | /** |
||
261 | * Get a value by key |
||
262 | * |
||
263 | * @param string $key |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | public function getAppValue($key) { |
||
268 | $defaultValue = null; |
||
269 | |||
270 | if (array_key_exists($key, $this->defaults)) { |
||
271 | $defaultValue = $this->defaults[$key]; |
||
272 | } |
||
273 | |||
274 | return $this->config->getAppValue($this->appName, $key, $defaultValue); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Set a value by key |
||
279 | * |
||
280 | * @param string $key |
||
281 | * @param string $value |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | public function setAppValue($key, $value) { |
||
286 | $this->config->setAppValue($this->appName, $key, $value); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * remove a key |
||
291 | * |
||
292 | * @param string $key |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function deleteAppValue($key) { |
||
297 | return $this->config->deleteAppValue($this->appName, $key); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Get a user value by key |
||
302 | * |
||
303 | * @param string $key |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getUserValue($key) { |
||
308 | return $this->config->getUserValue($this->userId, $this->appName, $key); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Set a user value by key |
||
313 | * |
||
314 | * @param string $key |
||
315 | * @param string $value |
||
316 | * |
||
317 | * @return string |
||
318 | * @throws PreConditionNotMetException |
||
319 | */ |
||
320 | public function setUserValue($key, $value) { |
||
321 | return $this->config->setUserValue($this->userId, $this->appName, $key, $value); |
||
322 | } |
||
323 | |||
324 | |||
325 | /** |
||
326 | * Get a user value by key and user |
||
327 | * |
||
328 | * @param string $userId |
||
329 | * @param string $key |
||
330 | * |
||
331 | * @param string $default |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
336 | return $this->config->getUserValue($userId, 'core', $key, $default); |
||
337 | } |
||
338 | |||
339 | |||
340 | /** |
||
341 | * Get a user value by key and user |
||
342 | * |
||
343 | * @param string $userId |
||
344 | * @param string $key |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | public function getValueForUser($userId, $key) { |
||
349 | return $this->config->getUserValue($userId, $this->appName, $key); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Set a user value by key |
||
354 | * |
||
355 | * @param string $userId |
||
356 | * @param string $key |
||
357 | * @param string $value |
||
358 | * |
||
359 | * @return string |
||
360 | * @throws PreConditionNotMetException |
||
361 | */ |
||
362 | public function setValueForUser($userId, $key, $value) { |
||
363 | return $this->config->setUserValue($userId, $this->appName, $key, $value); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * return the cloud version. |
||
368 | * if $complete is true, return a string x.y.z |
||
369 | * |
||
370 | * @param boolean $complete |
||
371 | * |
||
372 | * @return string|integer |
||
373 | */ |
||
374 | public function getCloudVersion($complete = false) { |
||
375 | $ver = Util::getVersion(); |
||
376 | |||
377 | if ($complete) { |
||
378 | return implode('.', $ver); |
||
379 | } |
||
380 | |||
381 | return $ver[0]; |
||
382 | } |
||
383 | |||
384 | |||
385 | /** |
||
386 | * @return bool |
||
387 | */ |
||
388 | public function isAccountOnly() { |
||
389 | return ($this->getAppValue(self::CIRCLES_ACCOUNTS_ONLY) === '1'); |
||
390 | } |
||
391 | |||
392 | |||
393 | /** |
||
394 | * @return bool |
||
395 | */ |
||
396 | public function isContactsBackend(): bool { |
||
397 | return ($this->getAppValue(ConfigService::CIRCLES_CONTACT_BACKEND) !== '0'); |
||
398 | } |
||
399 | |||
400 | |||
401 | public function contactsBackendType(): int { |
||
402 | return (int)$this->getAppValue(ConfigService::CIRCLES_CONTACT_BACKEND); |
||
403 | } |
||
404 | |||
405 | |||
406 | /** |
||
407 | * @return bool |
||
408 | */ |
||
409 | public function stillFrontEnd(): bool { |
||
410 | if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) !== '1') { |
||
411 | return true; |
||
412 | } |
||
413 | |||
414 | if ($this->getAppValue(self::CIRCLES_STILL_FRONTEND) === '1') { |
||
415 | return true; |
||
416 | } |
||
417 | |||
418 | return false; |
||
419 | } |
||
420 | |||
421 | |||
422 | /** |
||
423 | * should the password for a mail share be send to the recipient |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function sendPasswordByMail() { |
||
428 | if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) === '1') { |
||
429 | return false; |
||
430 | } |
||
431 | |||
432 | return ($this->config->getAppValue('sharebymail', 'sendpasswordmail', 'yes') === 'yes'); |
||
433 | } |
||
434 | |||
435 | /** |
||
454 |