Completed
Push — master ( 5a32ab...597ac5 )
by Robbie
10s
created

Bootstrap   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 65
ccs 16
cts 19
cp 0.8421
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 9 2
A findSilverStripe() 0 14 3
A getDb() 0 8 2
A defineHttpHost() 0 6 1
1
<?php
2
3
namespace SilverLeague\Console\Framework;
4
5
use SilverStripe\ORM\DB;
6
7
/**
8
 * Loads and configures SilverStripe
9
 *
10
 * @package silverstripe-console
11
 * @author  Robbie Averill <[email protected]>
12
 */
13
class Bootstrap
14
{
15
    /**
16 3
     * Ensure SilverStripe is loaded and configured
17
     */
18 3
    public function initialize()
19
    {
20
        if (!$this->findSilverStripe()) {
21
            echo 'A SilverStripe installation could not be found. Please run ssconsole from your '
22
                . 'SilverStripe root.', PHP_EOL;
23 3
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method initialize() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
24 3
        }
25
        $this->getDb();
26
    }
27
28
    /**
29
     * Find and require SilverStripe. This will look in:
30
     *
31
     * - The current working directory (for when installed globally with composer)
32
     * - The next directory up (for when installed locally into a SilverStripe project)
33
     * - The console's "silverstripe" directory (for when installed in a build process)
34
     *
35 3
     * @return bool
36
     */
37 3
    protected function findSilverStripe()
38 3
    {
39 3
        foreach ([getcwd(), CONSOLE_BASE_DIR . '/../', CONSOLE_BASE_DIR . '/silverstripe'] as $rootFolder) {
40 3
            if (file_exists($rootFolder . '/framework/src/Core/Core.php')) {
41 3
                define('SILVERSTRIPE_ROOT_DIR', $rootFolder);
42 3
                $this->defineHttpHost();
43
44
                require_once $rootFolder . '/vendor/autoload.php';
45
                require_once $rootFolder . '/framework/src/Core/Core.php';
46
                return true;
47
            }
48
        }
49
        return false;
50
    }
51
52
    /**
53 3
     * Get the SilverStripe DB connector
54
     *
55 3
     * @return $this
56 3
     */
57 3
    protected function getDb()
58
    {
59 3
        global $databaseConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
60
        if ($databaseConfig) {
61
            DB::connect($databaseConfig);
62
        }
63
        return $this;
64
    }
65
66
    /**
67
     * Defines a mock file to URL map for the console so that controller routes will work
68
     *
69
     * @return $this
70
     */
71
    protected function defineHttpHost()
72
    {
73
        global $_FILE_TO_URL_MAPPING;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
74
        $_FILE_TO_URL_MAPPING[CONSOLE_BASE_DIR] = 'http://localhost';
75
        return $this;
76
    }
77
}
78