Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Connection extends LDAPUtility { |
||
| 55 | private $ldapConnectionRes = null; |
||
| 56 | private $configPrefix; |
||
| 57 | private $configID; |
||
| 58 | private $configured = false; |
||
| 59 | private $hasPagedResultSupport = true; |
||
| 60 | //whether connection should be kept on __destruct |
||
| 61 | private $dontDestruct = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool runtime flag that indicates whether supported primary groups are available |
||
| 65 | */ |
||
| 66 | public $hasPrimaryGroups = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var bool runtime flag that indicates whether supported POSIX gidNumber are available |
||
| 70 | */ |
||
| 71 | public $hasGidNumber = true; |
||
| 72 | |||
| 73 | //cache handler |
||
| 74 | protected $cache; |
||
| 75 | |||
| 76 | /** @var Configuration settings handler **/ |
||
| 77 | protected $configuration; |
||
| 78 | |||
| 79 | protected $doNotValidate = false; |
||
| 80 | |||
| 81 | protected $ignoreValidation = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor |
||
| 85 | * @param ILDAPWrapper $ldap |
||
| 86 | * @param string $configPrefix a string with the prefix for the configkey column (appconfig table) |
||
| 87 | * @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections |
||
| 88 | */ |
||
| 89 | public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') { |
||
| 106 | |||
| 107 | public function __destruct() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * defines behaviour when the instance is cloned |
||
| 115 | */ |
||
| 116 | public function __clone() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $name |
||
| 125 | * @return bool|mixed|void |
||
| 126 | */ |
||
| 127 | public function __get($name) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $name |
||
| 141 | * @param mixed $value |
||
| 142 | */ |
||
| 143 | public function __set($name, $value) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * sets whether the result of the configuration validation shall |
||
| 158 | * be ignored when establishing the connection. Used by the Wizard |
||
| 159 | * in early configuration state. |
||
| 160 | * @param bool $state |
||
| 161 | */ |
||
| 162 | public function setIgnoreValidation($state) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * initializes the LDAP backend |
||
| 168 | * @param bool $force read the config settings no matter what |
||
| 169 | */ |
||
| 170 | public function init($force = false) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the LDAP handler |
||
| 177 | */ |
||
| 178 | public function getConnectionResource() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * resets the connection resource |
||
| 194 | */ |
||
| 195 | public function resetConnectionResource() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string|null $key |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | private function getCacheKey($key) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $key |
||
| 216 | * @return mixed|null |
||
| 217 | */ |
||
| 218 | public function getFromCache($key) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $key |
||
| 232 | * @param mixed $value |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function writeToCache($key, $value) { |
||
| 249 | |||
| 250 | public function clearCache() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Caches the general LDAP configuration. |
||
| 258 | * @param bool $force optional. true, if the re-read should be forced. defaults |
||
| 259 | * to false. |
||
| 260 | * @return null |
||
| 261 | */ |
||
| 262 | private function readConfiguration($force = false) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * set LDAP configuration with values delivered by an array, not read from configuration |
||
| 271 | * @param array $config array that holds the config parameters in an associated array |
||
| 272 | * @param array &$setParameters optional; array where the set fields will be given to |
||
| 273 | * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters |
||
| 274 | */ |
||
| 275 | public function setConfiguration($config, &$setParameters = null) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * saves the current Configuration in the database and empties the |
||
| 291 | * cache |
||
| 292 | * @return null |
||
| 293 | */ |
||
| 294 | public function saveConfiguration() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * get the current LDAP configuration |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function getConfiguration() { |
||
| 332 | |||
| 333 | private function doSoftValidation() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | private function doCriticalValidation() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Validates the user specified configuration |
||
| 471 | * @return bool true if configuration seems OK, false otherwise |
||
| 472 | */ |
||
| 473 | private function validateConfiguration() { |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * Connects and Binds to LDAP |
||
| 494 | */ |
||
| 495 | private function establishConnection() { |
||
| 496 | if(!$this->configuration->ldapConfigurationActive) { |
||
| 497 | return null; |
||
| 498 | } |
||
| 499 | static $phpLDAPinstalled = true; |
||
| 500 | if(!$phpLDAPinstalled) { |
||
| 501 | return false; |
||
| 502 | } |
||
| 503 | if(!$this->ignoreValidation && !$this->configured) { |
||
| 504 | \OCP\Util::writeLog('user_ldap', |
||
| 505 | 'Configuration is invalid, cannot connect', |
||
| 506 | \OCP\Util::WARN); |
||
| 507 | return false; |
||
| 508 | } |
||
| 509 | if(!$this->ldapConnectionRes) { |
||
| 510 | if(!$this->ldap->areLDAPFunctionsAvailable()) { |
||
| 511 | $phpLDAPinstalled = false; |
||
| 512 | \OCP\Util::writeLog('user_ldap', |
||
| 513 | 'function ldap_connect is not available. Make '. |
||
| 514 | 'sure that the PHP ldap module is installed.', |
||
| 515 | \OCP\Util::ERROR); |
||
| 516 | |||
| 517 | return false; |
||
| 518 | } |
||
| 519 | if($this->configuration->turnOffCertCheck) { |
||
| 520 | if(putenv('LDAPTLS_REQCERT=never')) { |
||
| 521 | \OCP\Util::writeLog('user_ldap', |
||
| 522 | 'Turned off SSL certificate validation successfully.', |
||
| 523 | \OCP\Util::DEBUG); |
||
| 524 | } else { |
||
| 525 | \OCP\Util::writeLog('user_ldap', |
||
| 526 | 'Could not turn off SSL certificate validation.', |
||
| 527 | \OCP\Util::WARN); |
||
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 531 | $isOverrideMainServer = ($this->configuration->ldapOverrideMainServer |
||
| 532 | || $this->getFromCache('overrideMainServer')); |
||
| 533 | $isBackupHost = (trim($this->configuration->ldapBackupHost) !== ""); |
||
| 534 | $bindStatus = false; |
||
| 535 | $error = -1; |
||
| 536 | try { |
||
| 537 | if (!$isOverrideMainServer) { |
||
| 538 | $this->doConnect($this->configuration->ldapHost, |
||
| 539 | $this->configuration->ldapPort); |
||
| 540 | $bindStatus = $this->bind(); |
||
| 541 | $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
||
| 542 | $this->ldap->errno($this->ldapConnectionRes) : -1; |
||
| 543 | } |
||
| 544 | if($bindStatus === true) { |
||
| 545 | return $bindStatus; |
||
| 546 | } |
||
| 547 | } catch (ServerNotAvailableException $e) { |
||
| 548 | if(!$isBackupHost) { |
||
| 549 | throw $e; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | |||
| 553 | //if LDAP server is not reachable, try the Backup (Replica!) Server |
||
| 554 | if($isBackupHost && ($error !== 0 || $isOverrideMainServer)) { |
||
| 555 | $this->doConnect($this->configuration->ldapBackupHost, |
||
| 556 | $this->configuration->ldapBackupPort); |
||
| 557 | $bindStatus = $this->bind(); |
||
| 558 | $error = $this->ldap->isResource($this->ldapConnectionRes) ? |
||
| 559 | $this->ldap->errno($this->ldapConnectionRes) : -1; |
||
| 560 | if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) { |
||
| 561 | //when bind to backup server succeeded and failed to main server, |
||
| 562 | //skip contacting him until next cache refresh |
||
| 563 | $this->writeToCache('overrideMainServer', true); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | return $bindStatus; |
||
| 568 | } |
||
| 569 | return null; |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param string $host |
||
| 574 | * @param string $port |
||
| 575 | * @return bool |
||
| 576 | * @throws \OC\ServerNotAvailableException |
||
| 577 | */ |
||
| 578 | private function doConnect($host, $port) { |
||
| 579 | if ($host === '') { |
||
| 580 | return false; |
||
| 581 | } |
||
| 582 | |||
| 583 | $this->ldapConnectionRes = $this->ldap->connect($host, $port); |
||
| 584 | |||
| 585 | if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) { |
||
| 586 | throw new ServerNotAvailableException('Could not set required LDAP Protocol version.'); |
||
| 587 | } |
||
| 588 | |||
| 589 | if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) { |
||
| 590 | throw new ServerNotAvailableException('Could not disable LDAP referrals.'); |
||
| 591 | } |
||
| 592 | |||
| 593 | if($this->configuration->ldapTLS) { |
||
| 594 | if(!$this->ldap->startTls($this->ldapConnectionRes)) { |
||
| 595 | throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.'); |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | return true; |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Binds to LDAP |
||
| 604 | */ |
||
| 605 | public function bind() { |
||
| 632 | |||
| 633 | } |
||
| 634 |
If you suppress an error, we recommend checking for the error condition explicitly: