Completed
Push — master ( 8c3306...57acb7 )
by Martijn van
02:51
created

ConsoleController::writeSuperSakeFileToWebRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
4
use Symfony\Component\Console\Application;
5
use Symfony\Component\Console\Input\ArgvInput;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Output\BufferedOutput;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Class ConsoleController
13
 * The Central Command Access Point and Bootstrapper.
14
 */
15
class ConsoleController extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
{
17
    /**
18
     * @var array
19
     */
20
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
        'publish',
22
    ];
23
24
    /**
25
     * @var SilverstripeApplication
26
     */
27
    protected $application;
28
29
    public function init()
30
    {
31
        parent::init();
32
33
        $this->application = new SilverstripeApplication();
34
    }
35
36
    public function index(SS_HTTPRequest $request)
37
    {
38
        /**
39
         * Lets be clear when calling commands
40
         */
41
        ini_set('display_errors', 1);
42
43
        if(!Director::is_cli()) {
44
            if(!Permission::check("ADMIN")) {
45
                return Security::permissionFailure();
46
            }
47
            return $this->callFromBrowser($request);
48
        }
49
50
        return $this->callFromConsole();
51
    }
52
53
    protected function callFromConsole()
0 ignored issues
show
Coding Style introduced by
callFromConsole 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...
54
    {
55
        // remove the framework/cli-script.php argument
56
        array_shift($_SERVER['argv']);
57
58
        $this->application->run(new ArgvInput($_SERVER['argv']), new ConsoleOutput());
59
    }
60
61
    protected function callFromBrowser(SS_HTTPRequest $request)
62
    {
63
        $input = new ArrayInput(array(
64
            'command' => $request->param('ID')
65
        ));
66
67
        $output = new BufferedOutput(
68
            OutputInterface::VERBOSITY_NORMAL,
69
            true // true for decorated
70
        );
71
        $this->application->run($input, $output);
72
73
        // return the output
74
        $converter = new AnsiToHtmlConverter();
75
        $content = $output->fetch();
76
        
77
        return ['ConsoleOutput' => $converter->convert($content)];
78
    }
79
}
80