Completed
Push — master ( 8110dc...01b890 )
by Robbie
01:10
created

Bootstrap::findSilverStripe()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
crap 3.0175
1
<?php
2
3
namespace SilverLeague\Console\Framework;
4
5
/**
6
 * Loads and configures SilverStripe
7
 *
8
 * @package silverstripe-console
9
 * @author  Robbie Averill <[email protected]>
10
 */
11
class Bootstrap
12
{
13
    /**
14
     * Ensure SilverStripe is loaded and configured
15
     */
16 1
    public function initialize()
17
    {
18 1
        if (!$this->findSilverStripe()) {
19
            echo 'A SilverStripe installation could not be found. Please run ssconsole from your '
20
                . 'SilverStripe root.', PHP_EOL;
21
            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...
22
        }
23 1
        $this->getDb();
24 1
    }
25
26
    /**
27
     * Find and require SilverStripe. This will look in:
28
     *
29
     * - The current working directory (for when installed globally with composer)
30
     * - The next directory up (for when installed locally into a SilverStripe project)
31
     * - The console's "silverstripe" directory (for when installed in a build process)
32
     *
33
     * @return bool
34
     */
35 1
    protected function findSilverStripe()
36
    {
37 1
        foreach ([getcwd(), CONSOLE_BASE_DIR . '/../', CONSOLE_BASE_DIR . '/silverstripe'] as $rootFolder) {
38 1
            if (file_exists($rootFolder . '/framework/src/Core/Core.php')) {
39 1
                define('SILVERSTRIPE_ROOT_DIR', $rootFolder);
40 1
                require_once $rootFolder . '/vendor/autoload.php';
41 1
                require_once $rootFolder . '/framework/src/Core/Core.php';
42 1
                return true;
43
            }
44
        }
45
        return false;
46
    }
47
48
    /**
49
     * Get the SilverStripe DB connector
50
     *
51
     * @return $this
52
     */
53 1
    protected function getDb()
54
    {
55 1
        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...
56 1
        if ($databaseConfig) {
57
            \SilverStripe\ORM\DB::connect($databaseConfig);
58
        }
59 1
        return $this;
60
    }
61
}
62