| Conditions | 38 |
| Paths | 2532 |
| Total Lines | 163 |
| Code Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 225 | public function doBuild($quiet = false, $populate = true, $testMode = false) |
||
| 226 | { |
||
| 227 | if ($quiet) { |
||
| 228 | DB::quiet(); |
||
| 229 | } else { |
||
| 230 | $conn = DB::get_conn(); |
||
| 231 | // Assumes database class is like "MySQLDatabase" or "MSSQLDatabase" (suffixed with "Database") |
||
| 232 | $dbType = substr(get_class($conn), 0, -8); |
||
| 233 | $dbVersion = $conn->getVersion(); |
||
| 234 | $databaseName = $conn->getSelectedDatabase(); |
||
| 235 | |||
| 236 | if (Director::is_cli()) { |
||
| 237 | echo sprintf("\n\nBuilding database %s using %s %s\n\n", $databaseName, $dbType, $dbVersion); |
||
| 238 | } else { |
||
| 239 | echo sprintf("<h2>Building database %s using %s %s</h2>", $databaseName, $dbType, $dbVersion); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | // Set up the initial database |
||
| 244 | if (!DB::is_active()) { |
||
| 245 | if (!$quiet) { |
||
| 246 | echo '<p><b>Creating database</b></p>'; |
||
| 247 | } |
||
| 248 | |||
| 249 | // Load parameters from existing configuration |
||
| 250 | $databaseConfig = DB::getConfig(); |
||
| 251 | if (empty($databaseConfig) && empty($_REQUEST['db'])) { |
||
| 252 | throw new BadMethodCallException("No database configuration available"); |
||
| 253 | } |
||
| 254 | $parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db']; |
||
| 255 | |||
| 256 | // Check database name is given |
||
| 257 | if (empty($parameters['database'])) { |
||
| 258 | throw new BadMethodCallException( |
||
| 259 | "No database name given; please give a value for SS_DATABASE_NAME or set SS_DATABASE_CHOOSE_NAME" |
||
| 260 | ); |
||
| 261 | } |
||
| 262 | $database = $parameters['database']; |
||
| 263 | |||
| 264 | // Establish connection and create database in two steps |
||
| 265 | unset($parameters['database']); |
||
| 266 | DB::connect($parameters); |
||
| 267 | DB::create_database($database); |
||
| 268 | } |
||
| 269 | |||
| 270 | // Build the database. Most of the hard work is handled by DataObject |
||
| 271 | $dataClasses = ClassInfo::subclassesFor(DataObject::class); |
||
| 272 | array_shift($dataClasses); |
||
| 273 | |||
| 274 | if (!$quiet) { |
||
| 275 | if (Director::is_cli()) { |
||
| 276 | echo "\nCREATING DATABASE TABLES\n\n"; |
||
| 277 | } else { |
||
| 278 | echo "\n<p><b>Creating database tables</b></p><ul>\n\n"; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | $showRecordCounts = (boolean)$this->config()->show_record_counts; |
||
| 283 | |||
| 284 | // Initiate schema update |
||
| 285 | $dbSchema = DB::get_schema(); |
||
| 286 | $dbSchema->schemaUpdate(function () use ($dataClasses, $testMode, $quiet, $showRecordCounts) { |
||
| 287 | $dataObjectSchema = DataObject::getSchema(); |
||
| 288 | |||
| 289 | foreach ($dataClasses as $dataClass) { |
||
| 290 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
| 291 | if (!class_exists($dataClass)) { |
||
| 292 | continue; |
||
| 293 | } |
||
| 294 | |||
| 295 | // Check if this class should be excluded as per testing conventions |
||
| 296 | $SNG = singleton($dataClass); |
||
| 297 | if (!$testMode && $SNG instanceof TestOnly) { |
||
| 298 | continue; |
||
| 299 | } |
||
| 300 | $tableName = $dataObjectSchema->tableName($dataClass); |
||
| 301 | |||
| 302 | // Log data |
||
| 303 | if (!$quiet) { |
||
| 304 | if ($showRecordCounts && DB::get_schema()->hasTable($tableName)) { |
||
| 305 | try { |
||
| 306 | $count = DB::query("SELECT COUNT(*) FROM \"$tableName\"")->value(); |
||
| 307 | $countSuffix = " ($count records)"; |
||
| 308 | } catch (Exception $e) { |
||
| 309 | $countSuffix = " (error getting record count)"; |
||
| 310 | } |
||
| 311 | } else { |
||
| 312 | $countSuffix = ""; |
||
| 313 | } |
||
| 314 | |||
| 315 | if (Director::is_cli()) { |
||
| 316 | echo " * $tableName$countSuffix\n"; |
||
| 317 | } else { |
||
| 318 | echo "<li>$tableName$countSuffix</li>\n"; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | // Instruct the class to apply its schema to the database |
||
| 323 | $SNG->requireTable(); |
||
| 324 | } |
||
| 325 | }); |
||
| 326 | ClassInfo::reset_db_cache(); |
||
| 327 | |||
| 328 | if (!$quiet && !Director::is_cli()) { |
||
| 329 | echo "</ul>"; |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($populate) { |
||
| 333 | if (!$quiet) { |
||
| 334 | if (Director::is_cli()) { |
||
| 335 | echo "\nCREATING DATABASE RECORDS\n\n"; |
||
| 336 | } else { |
||
| 337 | echo "\n<p><b>Creating database records</b></p><ul>\n\n"; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | // Remap obsolete class names |
||
| 342 | $remappingConfig = $this->config()->get('classname_value_remapping'); |
||
| 343 | $remappingFields = $this->getClassNameRemappingFields(); |
||
| 344 | foreach ($remappingFields as $className => $fieldNames) { |
||
| 345 | foreach ($fieldNames as $fieldName) { |
||
| 346 | foreach ($remappingConfig as $oldClassName => $newClassName) { |
||
| 347 | $this->updateLegacyClassNames($className, $fieldName, $oldClassName, $newClassName); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | // Require all default records |
||
| 353 | foreach ($dataClasses as $dataClass) { |
||
| 354 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
| 355 | // Test_ indicates that it's the data class is part of testing system |
||
| 356 | if (strpos($dataClass, 'Test_') === false && class_exists($dataClass)) { |
||
| 357 | if (!$quiet) { |
||
| 358 | if (Director::is_cli()) { |
||
| 359 | echo " * $dataClass\n"; |
||
| 360 | } else { |
||
| 361 | echo "<li>$dataClass</li>\n"; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | DataObject::singleton($dataClass)->requireDefaultRecords(); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | if (!$quiet && !Director::is_cli()) { |
||
| 370 | echo "</ul>"; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | touch(TEMP_PATH |
||
| 375 | . DIRECTORY_SEPARATOR |
||
| 376 | . 'database-last-generated-' |
||
| 377 | . str_replace(array('\\', '/', ':'), '.', Director::baseFolder())); |
||
| 378 | |||
| 379 | if (isset($_REQUEST['from_installer'])) { |
||
| 380 | echo "OK"; |
||
| 381 | } |
||
| 382 | |||
| 383 | if (!$quiet) { |
||
| 384 | echo (Director::is_cli()) ? "\n Database build completed!\n\n" :"<p>Database build completed!</p>"; |
||
| 385 | } |
||
| 386 | |||
| 387 | ClassInfo::reset_db_cache(); |
||
| 388 | } |
||
| 518 |