Complex classes like Setup 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 Setup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Setup { |
||
| 50 | /** @var \OCP\IConfig */ |
||
| 51 | protected $config; |
||
| 52 | /** @var IniGetWrapper */ |
||
| 53 | protected $iniWrapper; |
||
| 54 | /** @var IL10N */ |
||
| 55 | protected $l10n; |
||
| 56 | /** @var \OC_Defaults */ |
||
| 57 | protected $defaults; |
||
| 58 | /** @var ILogger */ |
||
| 59 | protected $logger; |
||
| 60 | /** @var ISecureRandom */ |
||
| 61 | protected $random; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param IConfig $config |
||
| 65 | * @param IniGetWrapper $iniWrapper |
||
| 66 | * @param \OC_Defaults $defaults |
||
| 67 | */ |
||
| 68 | function __construct(IConfig $config, |
||
| 82 | |||
| 83 | static $dbSetupClasses = array( |
||
| 84 | 'mysql' => '\OC\Setup\MySQL', |
||
| 85 | 'pgsql' => '\OC\Setup\PostgreSQL', |
||
| 86 | 'oci' => '\OC\Setup\OCI', |
||
| 87 | 'sqlite' => '\OC\Setup\Sqlite', |
||
| 88 | 'sqlite3' => '\OC\Setup\Sqlite', |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Wrapper around the "class_exists" PHP function to be able to mock it |
||
| 93 | * @param string $name |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | protected function class_exists($name) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Wrapper around the "is_callable" PHP function to be able to mock it |
||
| 102 | * @param string $name |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | protected function is_callable($name) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Wrapper around \PDO::getAvailableDrivers |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | protected function getAvailableDbDriversForPdo() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the available and supported databases of this instance |
||
| 120 | * |
||
| 121 | * @param bool $allowAllDatabases |
||
| 122 | * @return array |
||
| 123 | * @throws Exception |
||
| 124 | */ |
||
| 125 | public function getSupportedDatabases($allowAllDatabases = false) { |
||
| 126 | $availableDatabases = array( |
||
| 127 | 'sqlite' => array( |
||
| 128 | 'type' => 'class', |
||
| 129 | 'call' => 'SQLite3', |
||
| 130 | 'name' => 'SQLite' |
||
| 131 | ), |
||
| 132 | 'mysql' => array( |
||
| 133 | 'type' => 'pdo', |
||
| 134 | 'call' => 'mysql', |
||
| 135 | 'name' => 'MySQL/MariaDB' |
||
| 136 | ), |
||
| 137 | 'pgsql' => array( |
||
| 138 | 'type' => 'pdo', |
||
| 139 | 'call' => 'pgsql', |
||
| 140 | 'name' => 'PostgreSQL' |
||
| 141 | ), |
||
| 142 | 'oci' => array( |
||
| 143 | 'type' => 'function', |
||
| 144 | 'call' => 'oci_connect', |
||
| 145 | 'name' => 'Oracle' |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | if ($allowAllDatabases) { |
||
| 149 | $configuredDatabases = array_keys($availableDatabases); |
||
| 150 | } else { |
||
| 151 | $configuredDatabases = $this->config->getSystemValue('supportedDatabases', |
||
| 152 | array('sqlite', 'mysql', 'pgsql')); |
||
| 153 | } |
||
| 154 | if(!is_array($configuredDatabases)) { |
||
| 155 | throw new Exception('Supported databases are not properly configured.'); |
||
| 156 | } |
||
| 157 | |||
| 158 | $supportedDatabases = array(); |
||
| 159 | |||
| 160 | foreach($configuredDatabases as $database) { |
||
| 161 | if(array_key_exists($database, $availableDatabases)) { |
||
| 162 | $working = false; |
||
| 163 | $type = $availableDatabases[$database]['type']; |
||
| 164 | $call = $availableDatabases[$database]['call']; |
||
| 165 | |||
| 166 | if($type === 'class') { |
||
| 167 | $working = $this->class_exists($call); |
||
| 168 | } elseif ($type === 'function') { |
||
| 169 | $working = $this->is_callable($call); |
||
| 170 | } elseif($type === 'pdo') { |
||
| 171 | $working = in_array($call, $this->getAvailableDbDriversForPdo(), TRUE); |
||
| 172 | } |
||
| 173 | if($working) { |
||
| 174 | $supportedDatabases[$database] = $availableDatabases[$database]['name']; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return $supportedDatabases; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Gathers system information like database type and does |
||
| 184 | * a few system checks. |
||
| 185 | * |
||
| 186 | * @return array of system info, including an "errors" value |
||
| 187 | * in case of errors/warnings |
||
| 188 | */ |
||
| 189 | public function getSystemInfo($allowAllDatabases = false) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $options |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function install($options) { |
||
| 258 | $l = $this->l10n; |
||
| 259 | |||
| 260 | $error = array(); |
||
| 261 | $dbType = $options['dbtype']; |
||
| 262 | |||
| 263 | if(empty($options['adminlogin'])) { |
||
| 264 | $error[] = $l->t('Set an admin username.'); |
||
| 265 | } |
||
| 266 | if(empty($options['adminpass'])) { |
||
| 267 | $error[] = $l->t('Set an admin password.'); |
||
| 268 | } |
||
| 269 | if(empty($options['directory'])) { |
||
| 270 | $options['directory'] = \OC::$SERVERROOT."/data"; |
||
| 271 | } |
||
| 272 | |||
| 273 | if (!isset(self::$dbSetupClasses[$dbType])) { |
||
| 274 | $dbType = 'sqlite'; |
||
| 275 | } |
||
| 276 | |||
| 277 | $username = htmlspecialchars_decode($options['adminlogin']); |
||
| 278 | $password = htmlspecialchars_decode($options['adminpass']); |
||
| 279 | $dataDir = htmlspecialchars_decode($options['directory']); |
||
| 280 | |||
| 281 | $class = self::$dbSetupClasses[$dbType]; |
||
| 282 | /** @var \OC\Setup\AbstractDatabase $dbSetup */ |
||
| 283 | $dbSetup = new $class($l, 'db_structure.xml', $this->config, |
||
| 284 | $this->logger, $this->random); |
||
| 285 | $error = array_merge($error, $dbSetup->validate($options)); |
||
| 286 | |||
| 287 | // validate the data directory |
||
| 288 | if ( |
||
| 289 | (!is_dir($dataDir) and !mkdir($dataDir)) or |
||
| 290 | !is_writable($dataDir) |
||
| 291 | ) { |
||
| 292 | $error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); |
||
| 293 | } |
||
| 294 | |||
| 295 | if(count($error) != 0) { |
||
| 296 | return $error; |
||
| 297 | } |
||
| 298 | |||
| 299 | $request = \OC::$server->getRequest(); |
||
| 300 | |||
| 301 | //no errors, good |
||
| 302 | if(isset($options['trusted_domains']) |
||
| 303 | && is_array($options['trusted_domains'])) { |
||
| 304 | $trustedDomains = $options['trusted_domains']; |
||
| 305 | } else { |
||
| 306 | $trustedDomains = [$request->getInsecureServerHost()]; |
||
| 307 | } |
||
| 308 | |||
| 309 | //use sqlite3 when available, otherwise sqlite2 will be used. |
||
| 310 | if($dbType=='sqlite' and class_exists('SQLite3')) { |
||
| 311 | $dbType='sqlite3'; |
||
| 312 | } |
||
| 313 | |||
| 314 | //generate a random salt that is used to salt the local user passwords |
||
| 315 | $salt = $this->random->generate(30); |
||
| 316 | // generate a secret |
||
| 317 | $secret = $this->random->generate(48); |
||
| 318 | |||
| 319 | //write the config file |
||
| 320 | $this->config->setSystemValues([ |
||
| 321 | 'passwordsalt' => $salt, |
||
| 322 | 'secret' => $secret, |
||
| 323 | 'trusted_domains' => $trustedDomains, |
||
| 324 | 'datadirectory' => $dataDir, |
||
| 325 | 'overwrite.cli.url' => $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT, |
||
| 326 | 'dbtype' => $dbType, |
||
| 327 | 'version' => implode('.', \OCP\Util::getVersion()), |
||
| 328 | ]); |
||
| 329 | |||
| 330 | try { |
||
| 331 | $dbSetup->initialize($options); |
||
| 332 | $dbSetup->setupDatabase($username); |
||
| 333 | } catch (\OC\DatabaseSetupException $e) { |
||
| 334 | $error[] = array( |
||
| 335 | 'error' => $e->getMessage(), |
||
| 336 | 'hint' => $e->getHint() |
||
| 337 | ); |
||
| 338 | return($error); |
||
| 339 | } catch (Exception $e) { |
||
| 340 | $error[] = array( |
||
| 341 | 'error' => 'Error while trying to create admin user: ' . $e->getMessage(), |
||
| 342 | 'hint' => '' |
||
| 343 | ); |
||
| 344 | return($error); |
||
| 345 | } |
||
| 346 | |||
| 347 | //create the user and group |
||
| 348 | $user = null; |
||
| 349 | try { |
||
| 350 | $user = \OC::$server->getUserManager()->createUser($username, $password); |
||
| 351 | if (!$user) { |
||
| 352 | $error[] = "User <$username> could not be created."; |
||
| 353 | } |
||
| 354 | } catch(Exception $exception) { |
||
| 355 | $error[] = $exception->getMessage(); |
||
| 356 | } |
||
| 357 | |||
| 358 | if(count($error) == 0) { |
||
| 359 | $config = \OC::$server->getConfig(); |
||
| 360 | $config->setAppValue('core', 'installedat', microtime(true)); |
||
| 361 | $config->setAppValue('core', 'lastupdatedat', microtime(true)); |
||
| 362 | $config->setAppValue('core', 'vendor', $this->getVendor()); |
||
| 363 | |||
| 364 | $group =\OC::$server->getGroupManager()->createGroup('admin'); |
||
| 365 | $group->addUser($user); |
||
| 366 | |||
| 367 | // Create a session token for the newly created user |
||
| 368 | // The token provider requires a working db, so it's not injected on setup |
||
| 369 | /* @var $userSession User\Session */ |
||
| 370 | $userSession = \OC::$server->getUserSession(); |
||
| 371 | $defaultTokenProvider = \OC::$server->query('OC\Authentication\Token\DefaultTokenProvider'); |
||
| 372 | $userSession->setTokenProvider($defaultTokenProvider); |
||
| 373 | $userSession->login($username, $password); |
||
| 374 | $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password); |
||
| 375 | |||
| 376 | //guess what this does |
||
| 377 | Installer::installShippedApps(); |
||
| 378 | |||
| 379 | // create empty file in data dir, so we can later find |
||
| 380 | // out that this is indeed an ownCloud data directory |
||
| 381 | file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', ''); |
||
| 382 | |||
| 383 | // Update .htaccess files |
||
| 384 | Setup::updateHtaccess(); |
||
| 385 | Setup::protectDataDirectory(); |
||
| 386 | |||
| 387 | //try to write logtimezone |
||
| 388 | if (date_default_timezone_get()) { |
||
| 389 | $config->setSystemValue('logtimezone', date_default_timezone_get()); |
||
| 390 | } |
||
| 391 | |||
| 392 | self::installBackgroundJobs(); |
||
| 393 | |||
| 394 | //and we are done |
||
| 395 | $config->setSystemValue('installed', true); |
||
| 396 | } |
||
| 397 | |||
| 398 | return $error; |
||
| 399 | } |
||
| 400 | |||
| 401 | public static function installBackgroundJobs() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return string Absolute path to htaccess |
||
| 407 | */ |
||
| 408 | private function pathToHtaccess() { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Append the correct ErrorDocument path for Apache hosts |
||
| 414 | */ |
||
| 415 | public static function updateHtaccess() { |
||
| 480 | |||
| 481 | public static function protectDataDirectory() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Return vendor from which this version was published |
||
| 504 | * |
||
| 505 | * @return string Get the vendor |
||
| 506 | * |
||
| 507 | * Copy of \OC\Updater::getVendor() |
||
| 508 | */ |
||
| 509 | private function getVendor() { |
||
| 515 | } |
||
| 516 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.