| Total Complexity | 44 |
| Total Lines | 257 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like InstallConfig 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 InstallConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class InstallConfig |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * List of preferred DB classes in order |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $preferredDatabases = [ |
||
| 20 | 'MySQLPDODatabase', |
||
| 21 | 'MySQLDatabase', |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Get database config from the current environment |
||
| 26 | * |
||
| 27 | * @param array $request Request object |
||
| 28 | * @param array $databaseClasses Supported database config |
||
| 29 | * @param bool $realPassword Set to true to get the real password. If false, any non-posted |
||
| 30 | * password will be redacted as '********'. Note: Posted passwords are considered disclosed and |
||
| 31 | * never redacted. |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | public function getDatabaseConfig($request, $databaseClasses, $realPassword = true) |
||
| 35 | { |
||
| 36 | // Get config from request |
||
| 37 | if (isset($request['db']['type'])) { |
||
| 38 | $type = $request['db']['type']; |
||
| 39 | if (isset($request['db'][$type])) { |
||
| 40 | $config = $request['db'][$type]; |
||
| 41 | // The posted placeholder must be substituted with the real password |
||
| 42 | if (isset($config['password']) && $config['password'] === Installer::PASSWORD_PLACEHOLDER) { |
||
| 43 | $config['password'] = Environment::getEnv('SS_DATABASE_PASSWORD') ?: ''; |
||
| 44 | } |
||
| 45 | return array_merge([ 'type' => $type ], $config); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | // Guess database config |
||
| 50 | return [ |
||
| 51 | 'type' => $this->getDatabaseClass($databaseClasses), |
||
| 52 | 'server' => Environment::getEnv('SS_DATABASE_SERVER') ?: 'localhost', |
||
| 53 | 'username' => Environment::getEnv('SS_DATABASE_USERNAME') ?: 'root', |
||
| 54 | 'password' => $realPassword |
||
| 55 | ? (Environment::getEnv('SS_DATABASE_PASSWORD') ?: '') |
||
| 56 | : Installer::PASSWORD_PLACEHOLDER, // Avoid password disclosure |
||
| 57 | 'database' => Environment::getEnv('SS_DATABASE_NAME') ?: 'SS_mysite', |
||
| 58 | 'path' => Environment::getEnv('SS_DATABASE_PATH') |
||
| 59 | ?: Environment::getEnv('SS_SQLITE_DATABASE_PATH') // sqlite compat |
||
| 60 | ?: null, |
||
| 61 | 'key' => Environment::getEnv('SS_DATABASE_KEY') |
||
| 62 | ?: Environment::getEnv('SS_SQLITE_DATABASE_KEY') // sqlite compat |
||
| 63 | ?: null, |
||
| 64 | ]; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get admin config from the environment |
||
| 69 | * |
||
| 70 | * @param array $request |
||
| 71 | * @param bool $realPassword Set to true to get the real password. If false, any non-posted |
||
| 72 | * password will be redacted as '********'. Note: Posted passwords are considered disclosed and |
||
| 73 | * never redacted. |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function getAdminConfig($request, $realPassword = true) |
||
| 77 | { |
||
| 78 | if (isset($request['admin'])) { |
||
| 79 | $config = $request['admin']; |
||
| 80 | if (isset($config['password']) && $config['password'] === Installer::PASSWORD_PLACEHOLDER) { |
||
| 81 | $config['password'] = Environment::getEnv('SS_DEFAULT_ADMIN_PASSWORD') ?: ''; |
||
| 82 | } |
||
| 83 | return $request['admin']; |
||
| 84 | } |
||
| 85 | |||
| 86 | return [ |
||
| 87 | 'username' => Environment::getEnv('SS_DEFAULT_ADMIN_USERNAME') ?: 'admin', |
||
| 88 | 'password' => $realPassword |
||
| 89 | ? (Environment::getEnv('SS_DEFAULT_ADMIN_PASSWORD') ?: '') |
||
| 90 | : Installer::PASSWORD_PLACEHOLDER, // Avoid password disclosure |
||
| 91 | ]; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Check if this site has already been installed |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public function alreadyInstalled() |
||
| 100 | { |
||
| 101 | if (file_exists($this->getEnvPath())) { |
||
| 102 | return true; |
||
| 103 | } |
||
| 104 | if (!file_exists($this->getConfigPath())) { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | $configContents = file_get_contents($this->getConfigPath()); |
||
| 108 | if (strstr($configContents, '$databaseConfig')) { |
||
|
|
|||
| 109 | return true; |
||
| 110 | } |
||
| 111 | if (strstr($configContents, '$database')) { |
||
| 112 | return true; |
||
| 113 | } |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | protected function getConfigPath() |
||
| 121 | { |
||
| 122 | return BASE_PATH . '/mysite/_config.php'; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | protected function getEnvPath() |
||
| 129 | { |
||
| 130 | return BASE_PATH . '/.env'; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Database configs available for configuration |
||
| 135 | * |
||
| 136 | * @param array $databaseClasses |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | protected function getDatabaseClass($databaseClasses) |
||
| 140 | { |
||
| 141 | $envDatabase = Environment::getEnv('SS_DATABASE_CLASS'); |
||
| 142 | if ($envDatabase) { |
||
| 143 | return $envDatabase; |
||
| 144 | } |
||
| 145 | |||
| 146 | // Check supported versions |
||
| 147 | foreach ($this->preferredDatabases as $candidate) { |
||
| 148 | if (!empty($databaseClasses[$candidate]['supported'])) { |
||
| 149 | return $candidate; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | return null; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get string representation of the framework version |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function getFrameworkVersion() |
||
| 161 | { |
||
| 162 | $composerLockPath = BASE_PATH . '/composer.lock'; |
||
| 163 | if (!file_exists($composerLockPath)) { |
||
| 164 | return 'unknown'; |
||
| 165 | } |
||
| 166 | $lockData = json_decode(file_get_contents($composerLockPath), true); |
||
| 167 | if (json_last_error() || empty($lockData['packages'])) { |
||
| 168 | return 'unknown'; |
||
| 169 | } |
||
| 170 | foreach ($lockData['packages'] as $package) { |
||
| 171 | if ($package['name'] === 'silverstripe/framework') { |
||
| 172 | return $package['version']; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | return 'unknown'; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Check if stats should be sent |
||
| 180 | * |
||
| 181 | * @param array $request |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function canSendStats($request) |
||
| 185 | { |
||
| 186 | return !empty($request['stats']); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get configured locales |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function getLocales() |
||
| 195 | { |
||
| 196 | return [ |
||
| 197 | 'af_ZA' => 'Afrikaans (South Africa)', |
||
| 198 | 'ar_EG' => 'Arabic (Egypt)', |
||
| 199 | 'hy_AM' => 'Armenian (Armenia)', |
||
| 200 | 'ast_ES' => 'Asturian (Spain)', |
||
| 201 | 'az_AZ' => 'Azerbaijani (Azerbaijan)', |
||
| 202 | 'bs_BA' => 'Bosnian (Bosnia and Herzegovina)', |
||
| 203 | 'bg_BG' => 'Bulgarian (Bulgaria)', |
||
| 204 | 'ca_ES' => 'Catalan (Spain)', |
||
| 205 | 'zh_CN' => 'Chinese (China)', |
||
| 206 | 'zh_TW' => 'Chinese (Taiwan)', |
||
| 207 | 'hr_HR' => 'Croatian (Croatia)', |
||
| 208 | 'cs_CZ' => 'Czech (Czech Republic)', |
||
| 209 | 'da_DK' => 'Danish (Denmark)', |
||
| 210 | 'nl_NL' => 'Dutch (Netherlands)', |
||
| 211 | 'en_GB' => 'English (United Kingdom)', |
||
| 212 | 'en_US' => 'English (United States)', |
||
| 213 | 'eo_XX' => 'Esperanto', |
||
| 214 | 'et_EE' => 'Estonian (Estonia)', |
||
| 215 | 'fo_FO' => 'Faroese (Faroe Islands)', |
||
| 216 | 'fi_FI' => 'Finnish (Finland)', |
||
| 217 | 'fr_FR' => 'French (France)', |
||
| 218 | 'de_DE' => 'German (Germany)', |
||
| 219 | 'el_GR' => 'Greek (Greece)', |
||
| 220 | 'he_IL' => 'Hebrew (Israel)', |
||
| 221 | 'hu_HU' => 'Hungarian (Hungary)', |
||
| 222 | 'is_IS' => 'Icelandic (Iceland)', |
||
| 223 | 'id_ID' => 'Indonesian (Indonesia)', |
||
| 224 | 'it_IT' => 'Italian (Italy)', |
||
| 225 | 'ja_JP' => 'Japanese (Japan)', |
||
| 226 | 'km_KH' => 'Khmer (Cambodia)', |
||
| 227 | 'lc_XX' => 'LOLCAT', |
||
| 228 | 'lv_LV' => 'Latvian (Latvia)', |
||
| 229 | 'lt_LT' => 'Lithuanian (Lithuania)', |
||
| 230 | 'ms_MY' => 'Malay (Malaysia)', |
||
| 231 | 'mi_NZ' => 'Maori (New Zealand)', |
||
| 232 | 'ne_NP' => 'Nepali (Nepal)', |
||
| 233 | 'nb_NO' => 'Norwegian', |
||
| 234 | 'fa_IR' => 'Persian (Iran)', |
||
| 235 | 'pl_PL' => 'Polish (Poland)', |
||
| 236 | 'pt_BR' => 'Portuguese (Brazil)', |
||
| 237 | 'pa_IN' => 'Punjabi (India)', |
||
| 238 | 'ro_RO' => 'Romanian (Romania)', |
||
| 239 | 'ru_RU' => 'Russian (Russia)', |
||
| 240 | 'sr_RS' => 'Serbian (Serbia)', |
||
| 241 | 'si_LK' => 'Sinhalese (Sri Lanka)', |
||
| 242 | 'sk_SK' => 'Slovak (Slovakia)', |
||
| 243 | 'sl_SI' => 'Slovenian (Slovenia)', |
||
| 244 | 'es_AR' => 'Spanish (Argentina)', |
||
| 245 | 'es_MX' => 'Spanish (Mexico)', |
||
| 246 | 'es_ES' => 'Spanish (Spain)', |
||
| 247 | 'sv_SE' => 'Swedish (Sweden)', |
||
| 248 | 'th_TH' => 'Thai (Thailand)', |
||
| 249 | 'tr_TR' => 'Turkish (Turkey)', |
||
| 250 | 'uk_UA' => 'Ukrainian (Ukraine)', |
||
| 251 | 'uz_UZ' => 'Uzbek (Uzbekistan)', |
||
| 252 | 'vi_VN' => 'Vietnamese (Vietnam)', |
||
| 253 | ]; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get theme selected |
||
| 258 | * |
||
| 259 | * @param $request |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getTheme($request) |
||
| 269 | } |
||
| 270 | } |
||
| 271 |