| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 238 |
| Code Lines | 152 |
| Lines | 8 |
| Ratio | 3.36 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 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 |
||
| 396 | private static function __install() |
||
| 397 | { |
||
| 398 | $fields = $_POST['fields']; |
||
| 399 | $start = time(); |
||
| 400 | |||
| 401 | Symphony::Log()->info('INSTALLATION PROCESS STARTED (' . DateTimeObj::get('c') . ')'); |
||
| 402 | |||
| 403 | // MySQL: Establishing connection |
||
| 404 | Symphony::Log()->info('MYSQL: Establishing Connection'); |
||
| 405 | |||
| 406 | try { |
||
| 407 | Symphony::Database()->connect( |
||
| 408 | $fields['database']['host'], |
||
| 409 | $fields['database']['user'], |
||
| 410 | $fields['database']['password'], |
||
| 411 | $fields['database']['port'], |
||
| 412 | $fields['database']['db'] |
||
| 413 | ); |
||
| 414 | } catch (DatabaseException $e) { |
||
| 415 | self::__abort( |
||
| 416 | 'There was a problem while trying to establish a connection to the MySQL server. Please check your settings.', |
||
| 417 | $start); |
||
| 418 | } |
||
| 419 | |||
| 420 | // MySQL: Setting prefix & character encoding |
||
| 421 | Symphony::Database()->setPrefix($fields['database']['tbl_prefix']); |
||
| 422 | |||
| 423 | // MySQL: Importing schema |
||
| 424 | Symphony::Log()->info('MYSQL: Importing Table Schema'); |
||
| 425 | |||
| 426 | try { |
||
| 427 | Symphony::Database()->import(file_get_contents(INSTALL . '/includes/install.sql')); |
||
| 428 | } catch (DatabaseException $e) { |
||
| 429 | self::__abort( |
||
| 430 | 'There was an error while trying to import data to the database. MySQL returned: ' . $e->getDatabaseErrorCode() . ': ' . $e->getDatabaseErrorMessage(), |
||
| 431 | $start); |
||
| 432 | } |
||
| 433 | |||
| 434 | // MySQL: Creating default author |
||
| 435 | Symphony::Log()->info('MYSQL: Creating Default Author'); |
||
| 436 | |||
| 437 | try { |
||
| 438 | Symphony::Database()->insert(array( |
||
| 439 | 'id' => 1, |
||
| 440 | 'username' => Symphony::Database()->cleanValue($fields['user']['username']), |
||
| 441 | 'password' => Cryptography::hash(Symphony::Database()->cleanValue($fields['user']['password'])), |
||
| 442 | 'first_name' => Symphony::Database()->cleanValue($fields['user']['firstname']), |
||
| 443 | 'last_name' => Symphony::Database()->cleanValue($fields['user']['lastname']), |
||
| 444 | 'email' => Symphony::Database()->cleanValue($fields['user']['email']), |
||
| 445 | 'last_seen' => null, |
||
| 446 | 'user_type' => 'developer', |
||
| 447 | 'primary' => 'yes', |
||
| 448 | 'default_area' => '/blueprints/sections/', |
||
| 449 | 'auth_token_active' => 'no' |
||
| 450 | ), 'tbl_authors'); |
||
| 451 | } catch (DatabaseException $e) { |
||
| 452 | self::__abort( |
||
| 453 | 'There was an error while trying create the default author. MySQL returned: ' . $e->getDatabaseErrorCode() . ': ' . $e->getDatabaseErrorMessage(), |
||
| 454 | $start); |
||
| 455 | } |
||
| 456 | |||
| 457 | // Configuration: Populating array |
||
| 458 | $conf = Symphony::Configuration()->get(); |
||
| 459 | |||
| 460 | foreach ($conf as $group => $settings) { |
||
| 461 | foreach ($settings as $key => $value) { |
||
| 462 | if (isset($fields[$group]) && isset($fields[$group][$key])) { |
||
| 463 | $conf[$group][$key] = $fields[$group][$key]; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | // Create manifest folder structure |
||
| 469 | Symphony::Log()->info('WRITING: Creating ‘manifest’ folder (/manifest)'); |
||
| 470 | if (!General::realiseDirectory(MANIFEST, $conf['directory']['write_mode'])) { |
||
| 471 | self::__abort( |
||
| 472 | 'Could not create ‘manifest’ directory. Check permission on the root folder.', |
||
| 473 | $start); |
||
| 474 | } |
||
| 475 | |||
| 476 | Symphony::Log()->info('WRITING: Creating ‘logs’ folder (/manifest/logs)'); |
||
| 477 | if (!General::realiseDirectory(LOGS, $conf['directory']['write_mode'])) { |
||
| 478 | self::__abort( |
||
| 479 | 'Could not create ‘logs’ directory. Check permission on /manifest.', |
||
| 480 | $start); |
||
| 481 | } |
||
| 482 | |||
| 483 | Symphony::Log()->info('WRITING: Creating ‘cache’ folder (/manifest/cache)'); |
||
| 484 | if (!General::realiseDirectory(CACHE, $conf['directory']['write_mode'])) { |
||
| 485 | self::__abort( |
||
| 486 | 'Could not create ‘cache’ directory. Check permission on /manifest.', |
||
| 487 | $start); |
||
| 488 | } |
||
| 489 | |||
| 490 | Symphony::Log()->info('WRITING: Creating ‘tmp’ folder (/manifest/tmp)'); |
||
| 491 | if (!General::realiseDirectory(MANIFEST . '/tmp', $conf['directory']['write_mode'])) { |
||
| 492 | self::__abort( |
||
| 493 | 'Could not create ‘tmp’ directory. Check permission on /manifest.', |
||
| 494 | $start); |
||
| 495 | } |
||
| 496 | |||
| 497 | // Writing configuration file |
||
| 498 | Symphony::Log()->info('WRITING: Configuration File'); |
||
| 499 | |||
| 500 | Symphony::Configuration()->setArray($conf); |
||
| 501 | |||
| 502 | View Code Duplication | if (!Symphony::Configuration()->write(CONFIG, $conf['file']['write_mode'])) { |
|
| 503 | self::__abort( |
||
| 504 | 'Could not create config file ‘' . CONFIG . '’. Check permission on /manifest.', |
||
| 505 | $start); |
||
| 506 | } |
||
| 507 | |||
| 508 | // Writing htaccess file |
||
| 509 | Symphony::Log()->info('CONFIGURING: Frontend', E_NOTICE); |
||
| 510 | |||
| 511 | $rewrite_base = ltrim(preg_replace('/\/install$/i', null, dirname($_SERVER['PHP_SELF'])), '/'); |
||
| 512 | $htaccess = str_replace( |
||
| 513 | '<!-- REWRITE_BASE -->', $rewrite_base, |
||
| 514 | file_get_contents(INSTALL . '/includes/htaccess.txt') |
||
| 515 | ); |
||
| 516 | |||
| 517 | if (!General::writeFile(DOCROOT . "/.htaccess", $htaccess, $conf['file']['write_mode'], 'a')) { |
||
| 518 | self::__abort( |
||
| 519 | 'Could not write ‘.htaccess’ file. Check permission on ' . DOCROOT, |
||
| 520 | $start); |
||
| 521 | } |
||
| 522 | |||
| 523 | // Writing /workspace folder |
||
| 524 | if (!is_dir(DOCROOT . '/workspace')) { |
||
| 525 | // Create workspace folder structure |
||
| 526 | Symphony::Log()->info('WRITING: Creating ‘workspace’ folder (/workspace)'); |
||
| 527 | if (!General::realiseDirectory(WORKSPACE, $conf['directory']['write_mode'])) { |
||
| 528 | self::__abort( |
||
| 529 | 'Could not create ‘workspace’ directory. Check permission on the root folder.', |
||
| 530 | $start); |
||
| 531 | } |
||
| 532 | |||
| 533 | Symphony::Log()->info('WRITING: Creating ‘data-sources’ folder (/workspace/data-sources)'); |
||
| 534 | if (!General::realiseDirectory(DATASOURCES, $conf['directory']['write_mode'])) { |
||
| 535 | self::__abort( |
||
| 536 | 'Could not create ‘workspace/data-sources’ directory. Check permission on the root folder.', |
||
| 537 | $start); |
||
| 538 | } |
||
| 539 | |||
| 540 | Symphony::Log()->info('WRITING: Creating ‘events’ folder (/workspace/events)'); |
||
| 541 | if (!General::realiseDirectory(EVENTS, $conf['directory']['write_mode'])) { |
||
| 542 | self::__abort( |
||
| 543 | 'Could not create ‘workspace/events’ directory. Check permission on the root folder.', |
||
| 544 | $start); |
||
| 545 | } |
||
| 546 | |||
| 547 | Symphony::Log()->info('WRITING: Creating ‘pages’ folder (/workspace/pages)'); |
||
| 548 | if (!General::realiseDirectory(PAGES, $conf['directory']['write_mode'])) { |
||
| 549 | self::__abort( |
||
| 550 | 'Could not create ‘workspace/pages’ directory. Check permission on the root folder.', |
||
| 551 | $start); |
||
| 552 | } |
||
| 553 | |||
| 554 | Symphony::Log()->info('WRITING: Creating ‘utilities’ folder (/workspace/utilities)'); |
||
| 555 | if (!General::realiseDirectory(UTILITIES, $conf['directory']['write_mode'])) { |
||
| 556 | self::__abort( |
||
| 557 | 'Could not create ‘workspace/utilities’ directory. Check permission on the root folder.', |
||
| 558 | $start); |
||
| 559 | } |
||
| 560 | } else { |
||
| 561 | Symphony::Log()->info('An existing ‘workspace’ directory was found at this location. Symphony will use this workspace.'); |
||
| 562 | |||
| 563 | // MySQL: Importing workspace data |
||
| 564 | Symphony::Log()->info('MYSQL: Importing Workspace Data...'); |
||
| 565 | |||
| 566 | if (is_file(WORKSPACE . '/install.sql')) { |
||
| 567 | try { |
||
| 568 | Symphony::Database()->import(file_get_contents(WORKSPACE . '/install.sql')); |
||
| 569 | } catch (DatabaseException $e) { |
||
| 570 | self::__abort( |
||
| 571 | 'There was an error while trying to import data to the database. MySQL returned: ' . $e->getDatabaseErrorCode() . ': ' . $e->getDatabaseErrorMessage(), |
||
| 572 | $start); |
||
| 573 | } |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | // Write extensions folder |
||
| 578 | if (!is_dir(EXTENSIONS)) { |
||
| 579 | // Create extensions folder |
||
| 580 | Symphony::Log()->info('WRITING: Creating ‘extensions’ folder (/extensions)'); |
||
| 581 | if (!General::realiseDirectory(EXTENSIONS, $conf['directory']['write_mode'])) { |
||
| 582 | self::__abort( |
||
| 583 | 'Could not create ‘extension’ directory. Check permission on the root folder.', |
||
| 584 | $start); |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | // Install existing extensions |
||
| 589 | Symphony::Log()->info('CONFIGURING: Installing existing extensions'); |
||
| 590 | $disabled_extensions = array(); |
||
| 591 | foreach (new DirectoryIterator(EXTENSIONS) as $e) { |
||
| 592 | if ($e->isDot() || $e->isFile() || !is_file($e->getRealPath() . '/extension.driver.php')) { |
||
| 593 | continue; |
||
| 594 | } |
||
| 595 | |||
| 596 | $handle = $e->getBasename(); |
||
| 597 | try { |
||
| 598 | if (!ExtensionManager::enable($handle)) { |
||
| 599 | $disabled_extensions[] = $handle; |
||
| 600 | Symphony::Log()->warning('Could not enable the extension ‘' . $handle . '’.'); |
||
| 601 | } |
||
| 602 | } catch (Exception $ex) { |
||
| 603 | $disabled_extensions[] = $handle; |
||
| 604 | Symphony::Log()->warning('Could not enable the extension ‘' . $handle . '’. '. $ex->getMessage()); |
||
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | // Loading default language |
||
| 609 | if (isset($_REQUEST['lang']) && $_REQUEST['lang'] !== 'en') { |
||
| 610 | Symphony::Log()->info('CONFIGURING: Default language'); |
||
| 611 | |||
| 612 | $language = Lang::Languages(); |
||
| 613 | $language = $language[$_REQUEST['lang']]; |
||
| 614 | |||
| 615 | // Is the language extension enabled? |
||
| 616 | if (in_array('lang_' . $language['handle'], ExtensionManager::listInstalledHandles())) { |
||
| 617 | Symphony::Configuration()->set('lang', $_REQUEST['lang'], 'symphony'); |
||
| 618 | View Code Duplication | if (!Symphony::Configuration()->write(CONFIG, $conf['file']['write_mode'])) { |
|
| 619 | Symphony::Log()->warning('Could not write default language ‘' . $language['name'] . '’ to config file.'); |
||
| 620 | } |
||
| 621 | } else { |
||
| 622 | Symphony::Log()->warning('Could not enable the desired language ‘' . $language['name'] . '’.'); |
||
| 623 | } |
||
| 624 | } |
||
| 625 | |||
| 626 | // Installation completed. Woo-hoo! |
||
| 627 | Symphony::Log()->info(sprintf('INSTALLATION COMPLETED: Execution Time - %d sec (%s)', |
||
| 628 | max(1, time() - $start), |
||
| 629 | date('d.m.y H:i:s') |
||
| 630 | )); |
||
| 631 | |||
| 632 | return $disabled_extensions; |
||
| 633 | } |
||
| 634 | |||
| 644 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: