| Conditions | 38 |
| Paths | 2532 |
| Total Lines | 171 |
| Code Lines | 97 |
| 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 |
||
| 218 | public function doBuild($quiet = false, $populate = true, $testMode = false) |
||
| 219 | { |
||
| 220 | if ($quiet) { |
||
| 221 | DB::quiet(); |
||
| 222 | } else { |
||
| 223 | $conn = DB::get_conn(); |
||
| 224 | // Assumes database class is like "MySQLDatabase" or "MSSQLDatabase" (suffixed with "Database") |
||
| 225 | $dbType = substr(get_class($conn), 0, -8); |
||
| 226 | $dbVersion = $conn->getVersion(); |
||
| 227 | $databaseName = $conn->getSelectedDatabase(); |
||
| 228 | |||
| 229 | if (Director::is_cli()) { |
||
| 230 | echo sprintf("\n\nBuilding database %s using %s %s\n\n", $databaseName, $dbType, $dbVersion); |
||
| 231 | } else { |
||
| 232 | echo sprintf("<h2>Building database %s using %s %s</h2>", $databaseName, $dbType, $dbVersion); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | // Set up the initial database |
||
| 237 | if (!DB::is_active()) { |
||
| 238 | if (!$quiet) { |
||
| 239 | echo '<p><b>Creating database</b></p>'; |
||
| 240 | } |
||
| 241 | |||
| 242 | // Load parameters from existing configuration |
||
| 243 | $databaseConfig = DB::getConfig(); |
||
| 244 | if (empty($databaseConfig) && empty($_REQUEST['db'])) { |
||
| 245 | throw new BadMethodCallException("No database configuration available"); |
||
| 246 | } |
||
| 247 | $parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db']; |
||
| 248 | |||
| 249 | // Check database name is given |
||
| 250 | if (empty($parameters['database'])) { |
||
| 251 | throw new BadMethodCallException( |
||
| 252 | "No database name given; please give a value for SS_DATABASE_NAME or set SS_DATABASE_CHOOSE_NAME" |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | $database = $parameters['database']; |
||
| 256 | |||
| 257 | // Establish connection and create database in two steps |
||
| 258 | unset($parameters['database']); |
||
| 259 | DB::connect($parameters); |
||
| 260 | DB::create_database($database); |
||
| 261 | } |
||
| 262 | |||
| 263 | // Build the database. Most of the hard work is handled by DataObject |
||
| 264 | $dataClasses = ClassInfo::subclassesFor(DataObject::class); |
||
| 265 | array_shift($dataClasses); |
||
| 266 | |||
| 267 | if (!$quiet) { |
||
| 268 | if (Director::is_cli()) { |
||
| 269 | echo "\nCREATING DATABASE TABLES\n\n"; |
||
| 270 | } else { |
||
| 271 | echo "\n<p><b>Creating database tables</b></p><ul>\n\n"; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | // Initiate schema update |
||
| 276 | $dbSchema = DB::get_schema(); |
||
| 277 | $dbSchema->schemaUpdate(function () use ($dataClasses, $testMode, $quiet) { |
||
| 278 | $dataObjectSchema = DataObject::getSchema(); |
||
| 279 | |||
| 280 | foreach ($dataClasses as $dataClass) { |
||
| 281 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
| 282 | if (!class_exists($dataClass)) { |
||
| 283 | continue; |
||
| 284 | } |
||
| 285 | |||
| 286 | // Check if this class should be excluded as per testing conventions |
||
| 287 | $SNG = singleton($dataClass); |
||
| 288 | if (!$testMode && $SNG instanceof TestOnly) { |
||
| 289 | continue; |
||
| 290 | } |
||
| 291 | $tableName = $dataObjectSchema->tableName($dataClass); |
||
| 292 | |||
| 293 | // Log data |
||
| 294 | if (!$quiet) { |
||
| 295 | if (Director::is_cli()) { |
||
| 296 | echo " * $tableName\n"; |
||
| 297 | } else { |
||
| 298 | echo "<li>$tableName</li>\n"; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | // Instruct the class to apply its schema to the database |
||
| 303 | $SNG->requireTable(); |
||
| 304 | } |
||
| 305 | }); |
||
| 306 | ClassInfo::reset_db_cache(); |
||
| 307 | |||
| 308 | if (!$quiet && !Director::is_cli()) { |
||
| 309 | echo "</ul>"; |
||
| 310 | } |
||
| 311 | |||
| 312 | if ($populate) { |
||
| 313 | if (!$quiet) { |
||
| 314 | if (Director::is_cli()) { |
||
| 315 | echo "\nCREATING DATABASE RECORDS\n\n"; |
||
| 316 | } else { |
||
| 317 | echo "\n<p><b>Creating database records</b></p><ul>\n\n"; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | foreach ($dataClasses as $dataClass) { |
||
| 322 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
| 323 | // Test_ indicates that it's the data class is part of testing system |
||
| 324 | if (strpos($dataClass, 'Test_') === false && class_exists($dataClass)) { |
||
| 325 | if (!$quiet) { |
||
| 326 | if (Director::is_cli()) { |
||
| 327 | echo " * $dataClass\n"; |
||
| 328 | } else { |
||
| 329 | echo "<li>$dataClass</li>\n"; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | singleton($dataClass)->requireDefaultRecords(); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | // Remap obsolete class names |
||
| 338 | $schema = DataObject::getSchema(); |
||
| 339 | foreach ($this->config()->classname_value_remapping as $oldClassName => $newClassName) { |
||
| 340 | $baseDataClass = $schema->baseDataClass($newClassName); |
||
| 341 | $badRecordCount = DataObject::get($baseDataClass) |
||
| 342 | ->filter(["ClassName" => $oldClassName ]) |
||
| 343 | ->count(); |
||
| 344 | if ($badRecordCount > 0) { |
||
| 345 | if (Director::is_cli()) { |
||
| 346 | echo " * Correcting $badRecordCount obsolete classname values for $newClassName\n"; |
||
| 347 | } else { |
||
| 348 | echo "<li>Correcting $badRecordCount obsolete classname values for $newClassName</li>\n"; |
||
| 349 | } |
||
| 350 | $table = $schema->baseDataTable($baseDataClass); |
||
| 351 | |||
| 352 | $updateQuery = "UPDATE \"$table%s\" SET \"ClassName\" = ? WHERE \"ClassName\" = ?"; |
||
| 353 | $updateQueries = [sprintf($updateQuery, '')]; |
||
| 354 | |||
| 355 | // Remap versioned table ClassName values as well |
||
| 356 | $class = singleton($newClassName); |
||
| 357 | if ($class->has_extension(Versioned::class)) { |
||
| 358 | if ($class->hasStages()) { |
||
| 359 | $updateQueries[] = sprintf($updateQuery, '_Live'); |
||
| 360 | } |
||
| 361 | $updateQueries[] = sprintf($updateQuery, '_Versions'); |
||
| 362 | } |
||
| 363 | |||
| 364 | foreach ($updateQueries as $query) { |
||
| 365 | DB::prepared_query($query, [$newClassName, $oldClassName]); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | if (!$quiet && !Director::is_cli()) { |
||
| 371 | echo "</ul>"; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | touch(TEMP_PATH |
||
| 376 | . DIRECTORY_SEPARATOR |
||
| 377 | . 'database-last-generated-' |
||
| 378 | . str_replace(array('\\', '/', ':'), '.', Director::baseFolder())); |
||
| 379 | |||
| 380 | if (isset($_REQUEST['from_installer'])) { |
||
| 381 | echo "OK"; |
||
| 382 | } |
||
| 383 | |||
| 384 | if (!$quiet) { |
||
| 385 | echo (Director::is_cli()) ? "\n Database build completed!\n\n" :"<p>Database build completed!</p>"; |
||
| 386 | } |
||
| 387 | |||
| 388 | ClassInfo::reset_db_cache(); |
||
| 389 | } |
||
| 444 |