Total Complexity | 94 |
Total Lines | 538 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Configuration 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.
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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Configuration { |
||
41 | const AVATAR_PREFIX_DEFAULT = 'default'; |
||
42 | const AVATAR_PREFIX_NONE = 'none'; |
||
43 | const AVATAR_PREFIX_DATA_ATTRIBUTE = 'data:'; |
||
44 | |||
45 | protected $configPrefix = null; |
||
46 | protected $configRead = false; |
||
47 | /** |
||
48 | * @var string[] pre-filled with one reference key so that at least one entry is written on save request and |
||
49 | * the config ID is registered |
||
50 | */ |
||
51 | protected $unsavedChanges = ['ldapConfigurationActive' => 'ldapConfigurationActive']; |
||
52 | |||
53 | //settings |
||
54 | protected $config = array( |
||
55 | 'ldapHost' => null, |
||
56 | 'ldapPort' => null, |
||
57 | 'ldapBackupHost' => null, |
||
58 | 'ldapBackupPort' => null, |
||
59 | 'ldapBase' => null, |
||
60 | 'ldapBaseUsers' => null, |
||
61 | 'ldapBaseGroups' => null, |
||
62 | 'ldapAgentName' => null, |
||
63 | 'ldapAgentPassword' => null, |
||
64 | 'ldapTLS' => null, |
||
65 | 'turnOffCertCheck' => null, |
||
66 | 'ldapIgnoreNamingRules' => null, |
||
67 | 'ldapUserDisplayName' => null, |
||
68 | 'ldapUserDisplayName2' => null, |
||
69 | 'ldapUserAvatarRule' => null, |
||
70 | 'ldapGidNumber' => null, |
||
71 | 'ldapUserFilterObjectclass' => null, |
||
72 | 'ldapUserFilterGroups' => null, |
||
73 | 'ldapUserFilter' => null, |
||
74 | 'ldapUserFilterMode' => null, |
||
75 | 'ldapGroupFilter' => null, |
||
76 | 'ldapGroupFilterMode' => null, |
||
77 | 'ldapGroupFilterObjectclass' => null, |
||
78 | 'ldapGroupFilterGroups' => null, |
||
79 | 'ldapGroupDisplayName' => null, |
||
80 | 'ldapGroupMemberAssocAttr' => null, |
||
81 | 'ldapLoginFilter' => null, |
||
82 | 'ldapLoginFilterMode' => null, |
||
83 | 'ldapLoginFilterEmail' => null, |
||
84 | 'ldapLoginFilterUsername' => null, |
||
85 | 'ldapLoginFilterAttributes' => null, |
||
86 | 'ldapQuotaAttribute' => null, |
||
87 | 'ldapQuotaDefault' => null, |
||
88 | 'ldapEmailAttribute' => null, |
||
89 | 'ldapCacheTTL' => null, |
||
90 | 'ldapUuidUserAttribute' => 'auto', |
||
91 | 'ldapUuidGroupAttribute' => 'auto', |
||
92 | 'ldapOverrideMainServer' => false, |
||
93 | 'ldapConfigurationActive' => false, |
||
94 | 'ldapAttributesForUserSearch' => null, |
||
95 | 'ldapAttributesForGroupSearch' => null, |
||
96 | 'ldapExperiencedAdmin' => false, |
||
97 | 'homeFolderNamingRule' => null, |
||
98 | 'hasMemberOfFilterSupport' => false, |
||
99 | 'useMemberOfToDetectMembership' => true, |
||
100 | 'ldapExpertUsernameAttr' => null, |
||
101 | 'ldapExpertUUIDUserAttr' => null, |
||
102 | 'ldapExpertUUIDGroupAttr' => null, |
||
103 | 'lastJpegPhotoLookup' => null, |
||
104 | 'ldapNestedGroups' => false, |
||
105 | 'ldapPagingSize' => null, |
||
106 | 'turnOnPasswordChange' => false, |
||
107 | 'ldapDynamicGroupMemberURL' => null, |
||
108 | 'ldapDefaultPPolicyDN' => null, |
||
109 | 'ldapExtStorageHomeAttribute' => null, |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * @param string $configPrefix |
||
114 | * @param bool $autoRead |
||
115 | */ |
||
116 | public function __construct($configPrefix, $autoRead = true) { |
||
117 | $this->configPrefix = $configPrefix; |
||
118 | if($autoRead) { |
||
119 | $this->readConfiguration(); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param string $name |
||
125 | * @return mixed|null |
||
126 | */ |
||
127 | public function __get($name) { |
||
128 | if(isset($this->config[$name])) { |
||
129 | return $this->config[$name]; |
||
130 | } |
||
131 | return null; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $name |
||
136 | * @param mixed $value |
||
137 | */ |
||
138 | public function __set($name, $value) { |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getConfiguration() { |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * set LDAP configuration with values delivered by an array, not read |
||
151 | * from configuration. It does not save the configuration! To do so, you |
||
152 | * must call saveConfiguration afterwards. |
||
153 | * @param array $config array that holds the config parameters in an associated |
||
154 | * array |
||
155 | * @param array &$applied optional; array where the set fields will be given to |
||
156 | * @return false|null |
||
157 | */ |
||
158 | public function setConfiguration($config, &$applied = null) { |
||
205 | } |
||
206 | |||
207 | public function readConfiguration() { |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * saves the current config changes in the database |
||
256 | */ |
||
257 | public function saveConfiguration() { |
||
258 | $cta = array_flip($this->getConfigTranslationArray()); |
||
259 | foreach($this->unsavedChanges as $key) { |
||
260 | $value = $this->config[$key]; |
||
261 | switch ($key) { |
||
262 | case 'ldapAgentPassword': |
||
263 | $value = base64_encode($value); |
||
264 | break; |
||
265 | case 'ldapBase': |
||
266 | case 'ldapBaseUsers': |
||
267 | case 'ldapBaseGroups': |
||
268 | case 'ldapAttributesForUserSearch': |
||
269 | case 'ldapAttributesForGroupSearch': |
||
270 | case 'ldapUserFilterObjectclass': |
||
271 | case 'ldapUserFilterGroups': |
||
272 | case 'ldapGroupFilterObjectclass': |
||
273 | case 'ldapGroupFilterGroups': |
||
274 | case 'ldapLoginFilterAttributes': |
||
275 | if(is_array($value)) { |
||
276 | $value = implode("\n", $value); |
||
277 | } |
||
278 | break; |
||
279 | //following options are not stored but detected, skip them |
||
280 | case 'ldapIgnoreNamingRules': |
||
281 | case 'ldapUuidUserAttribute': |
||
282 | case 'ldapUuidGroupAttribute': |
||
283 | continue 2; |
||
284 | } |
||
285 | if(is_null($value)) { |
||
286 | $value = ''; |
||
287 | } |
||
288 | $this->saveValue($cta[$key], $value); |
||
289 | } |
||
290 | $this->saveValue('_lastChange', time()); |
||
291 | $this->unsavedChanges = []; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param string $varName |
||
296 | * @return array|string |
||
297 | */ |
||
298 | protected function getMultiLine($varName) { |
||
299 | $value = $this->getValue($varName); |
||
300 | if(empty($value)) { |
||
301 | $value = ''; |
||
302 | } else { |
||
303 | $value = preg_split('/\r\n|\r|\n/', $value); |
||
304 | } |
||
305 | |||
306 | return $value; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Sets multi-line values as arrays |
||
311 | * |
||
312 | * @param string $varName name of config-key |
||
313 | * @param array|string $value to set |
||
314 | */ |
||
315 | protected function setMultiLine($varName, $value) { |
||
316 | if(empty($value)) { |
||
317 | $value = ''; |
||
318 | } else if (!is_array($value)) { |
||
319 | $value = preg_split('/\r\n|\r|\n|;/', $value); |
||
320 | if($value === false) { |
||
321 | $value = ''; |
||
322 | } |
||
323 | } |
||
324 | |||
325 | if(!is_array($value)) { |
||
326 | $finalValue = trim($value); |
||
327 | } else { |
||
328 | $finalValue = []; |
||
329 | foreach($value as $key => $val) { |
||
330 | if(is_string($val)) { |
||
331 | $val = trim($val); |
||
332 | if ($val !== '') { |
||
333 | //accidental line breaks are not wanted and can cause |
||
334 | // odd behaviour. Thus, away with them. |
||
335 | $finalValue[] = $val; |
||
336 | } |
||
337 | } else { |
||
338 | $finalValue[] = $val; |
||
339 | } |
||
340 | } |
||
341 | } |
||
342 | |||
343 | $this->setRawValue($varName, $finalValue); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param string $varName |
||
348 | * @return string |
||
349 | */ |
||
350 | protected function getPwd($varName) { |
||
351 | return base64_decode($this->getValue($varName)); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param string $varName |
||
356 | * @return string |
||
357 | */ |
||
358 | protected function getLcValue($varName) { |
||
359 | return mb_strtolower($this->getValue($varName), 'UTF-8'); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @param string $varName |
||
364 | * @return string |
||
365 | */ |
||
366 | protected function getSystemValue($varName) { |
||
367 | //FIXME: if another system value is added, softcode the default value |
||
368 | return \OC::$server->getConfig()->getSystemValue($varName, false); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @param string $varName |
||
373 | * @return string |
||
374 | */ |
||
375 | protected function getValue($varName) { |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Sets a scalar value. |
||
387 | * |
||
388 | * @param string $varName name of config key |
||
389 | * @param mixed $value to set |
||
390 | */ |
||
391 | protected function setValue($varName, $value) { |
||
392 | if(is_string($value)) { |
||
393 | $value = trim($value); |
||
394 | } |
||
395 | $this->config[$varName] = $value; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Sets a scalar value without trimming. |
||
400 | * |
||
401 | * @param string $varName name of config key |
||
402 | * @param mixed $value to set |
||
403 | */ |
||
404 | protected function setRawValue($varName, $value) { |
||
405 | $this->config[$varName] = $value; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @param string $varName |
||
410 | * @param string $value |
||
411 | * @return bool |
||
412 | */ |
||
413 | protected function saveValue($varName, $value) { |
||
414 | \OC::$server->getConfig()->setAppValue( |
||
415 | 'user_ldap', |
||
416 | $this->configPrefix.$varName, |
||
417 | $value |
||
418 | ); |
||
419 | return true; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @return array an associative array with the default values. Keys are correspond |
||
424 | * to config-value entries in the database table |
||
425 | */ |
||
426 | public function getDefaults() { |
||
427 | return array( |
||
428 | 'ldap_host' => '', |
||
429 | 'ldap_port' => '', |
||
430 | 'ldap_backup_host' => '', |
||
431 | 'ldap_backup_port' => '', |
||
432 | 'ldap_override_main_server' => '', |
||
433 | 'ldap_dn' => '', |
||
434 | 'ldap_agent_password' => '', |
||
435 | 'ldap_base' => '', |
||
436 | 'ldap_base_users' => '', |
||
437 | 'ldap_base_groups' => '', |
||
438 | 'ldap_userlist_filter' => '', |
||
439 | 'ldap_user_filter_mode' => 0, |
||
440 | 'ldap_userfilter_objectclass' => '', |
||
441 | 'ldap_userfilter_groups' => '', |
||
442 | 'ldap_login_filter' => '', |
||
443 | 'ldap_login_filter_mode' => 0, |
||
444 | 'ldap_loginfilter_email' => 0, |
||
445 | 'ldap_loginfilter_username' => 1, |
||
446 | 'ldap_loginfilter_attributes' => '', |
||
447 | 'ldap_group_filter' => '', |
||
448 | 'ldap_group_filter_mode' => 0, |
||
449 | 'ldap_groupfilter_objectclass' => '', |
||
450 | 'ldap_groupfilter_groups' => '', |
||
451 | 'ldap_gid_number' => 'gidNumber', |
||
452 | 'ldap_display_name' => 'displayName', |
||
453 | 'ldap_user_display_name_2' => '', |
||
454 | 'ldap_group_display_name' => 'cn', |
||
455 | 'ldap_tls' => 0, |
||
456 | 'ldap_quota_def' => '', |
||
457 | 'ldap_quota_attr' => '', |
||
458 | 'ldap_email_attr' => '', |
||
459 | 'ldap_group_member_assoc_attribute' => 'uniqueMember', |
||
460 | 'ldap_cache_ttl' => 600, |
||
461 | 'ldap_uuid_user_attribute' => 'auto', |
||
462 | 'ldap_uuid_group_attribute' => 'auto', |
||
463 | 'home_folder_naming_rule' => '', |
||
464 | 'ldap_turn_off_cert_check' => 0, |
||
465 | 'ldap_configuration_active' => 0, |
||
466 | 'ldap_attributes_for_user_search' => '', |
||
467 | 'ldap_attributes_for_group_search' => '', |
||
468 | 'ldap_expert_username_attr' => '', |
||
469 | 'ldap_expert_uuid_user_attr' => '', |
||
470 | 'ldap_expert_uuid_group_attr' => '', |
||
471 | 'has_memberof_filter_support' => 0, |
||
472 | 'use_memberof_to_detect_membership' => 1, |
||
473 | 'last_jpegPhoto_lookup' => 0, |
||
474 | 'ldap_nested_groups' => 0, |
||
475 | 'ldap_paging_size' => 500, |
||
476 | 'ldap_turn_on_pwd_change' => 0, |
||
477 | 'ldap_experienced_admin' => 0, |
||
478 | 'ldap_dynamic_group_member_url' => '', |
||
479 | 'ldap_default_ppolicy_dn' => '', |
||
480 | 'ldap_user_avatar_rule' => 'default', |
||
481 | 'ldap_ext_storage_home_attribute' => '', |
||
482 | ); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @return array that maps internal variable names to database fields |
||
487 | */ |
||
488 | public function getConfigTranslationArray() { |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * @param string $rule |
||
550 | * @return array |
||
551 | * @throws \RuntimeException |
||
552 | */ |
||
553 | public function resolveRule($rule) { |
||
558 | } |
||
559 | |||
560 | public function getAvatarAttributes() { |
||
561 | $value = $this->ldapUserAvatarRule ?: self::AVATAR_PREFIX_DEFAULT; |
||
578 | } |
||
579 | |||
580 | } |
||
581 |