Passed
Push — master ( f2e14b...2db21b )
by Robbie
04:12
created

Bootstrap::findSilverStripe()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 8
cts 9
cp 0.8889
rs 8.5806
cc 4
eloc 16
nc 4
nop 0
crap 4.0218
1
<?php
2
3
namespace SilverLeague\Console\Framework;
4
5
use SilverStripe\Control\HTTPApplication;
6
use SilverStripe\Control\HTTPRequestBuilder;
7
use SilverStripe\Core\CoreKernel;
8
use SilverStripe\Core\Startup\ErrorControlChainMiddleware;
9
use SilverStripe\ORM\DB;
10
11
/**
12
 * Loads and configures SilverStripe
13
 *
14
 * @package silverstripe-console
15
 * @author  Robbie Averill <[email protected]>
16 3
 */
17
class Bootstrap
18 3
{
19
    /**
20
     * Ensure SilverStripe is loaded and configured
21
     */
22
    public function initialize()
23 3
    {
24 3
        if (!$this->findSilverStripe()) {
25
            echo 'A SilverStripe installation could not be found. Please run ssconsole from your '
26
                . 'SilverStripe root.', PHP_EOL;
27
            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...
28
        }
29
    }
30
31
    /**
32
     * Find and require SilverStripe. This will look in:
33
     *
34
     * - The current working directory (for when installed globally with composer)
35 3
     * - The next directory up (for when installed locally into a SilverStripe project)
36
     * - The console's "silverstripe" directory (for when installed in a build process)
37 3
     *
38 3
     * @return bool
39 3
     */
40 3
    protected function findSilverStripe()
0 ignored issues
show
Coding Style introduced by
findSilverStripe uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41 3
    {
42 3
        if (defined('SILVERSTRIPE_ROOT_DIR')) {
43
            return true;
44
        }
45
46
        foreach ([getcwd(), CONSOLE_BASE_DIR . '/../', CONSOLE_BASE_DIR . '/silverstripe'] as $rootFolder) {
47
            if (file_exists($rootFolder . '/vendor/silverstripe/framework/src/Core/CoreKernel.php')) {
48
                define('SILVERSTRIPE_ROOT_DIR', $rootFolder);
49
50
                require_once $rootFolder . '/vendor/autoload.php';
51
52
                $_SERVER['REQUEST_URI'] = '/';
53 3
                $_SERVER['REQUEST_METHOD'] = 'GET';
54
55 3
                // Build request and detect flush
56 3
                $request = HTTPRequestBuilder::createFromEnvironment();
57 3
58
                // Default application
59 3
                $kernel = new CoreKernel(BASE_PATH);
60
                $app = new HTTPApplication($kernel);
61
                $app->addMiddleware(new ErrorControlChainMiddleware($app));
62
                $response = $app->handle($request);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
64
                return true;
65
            }
66
        }
67
68
        return false;
69
    }
70
}
71