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