Completed
Push — develop ( e4a933...b1fd58 )
by Tom
03:38
created

ServerEnvironment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mot
5
 * Date: 13.12.16
6
 * Time: 00:08
7
 */
8
9
namespace N98\Magento\Command\System\Cron;
10
11
use BadMethodCallException;
12
13
/**
14
 * Class ServerEnvironment
15
 *
16
 * Set $_SERVER environment for URL generating while sys:cron:run
17
 *
18
 * @see https://github.com/netz98/n98-magerun/issues/871
19
 *
20
 * @package N98\Magento\Command\System\Cron
21
 */
22
class ServerEnvironment
23
{
24
    /**
25
     * @var array
26
     */
27
    private $backup;
28
29
    /**
30
     * @var array
31
     */
32
    private $keys;
33
34
    public function __construct()
35
    {
36
        $this->keys = array('SCRIPT_NAME', 'SCRIPT_FILENAME');
37
    }
38
39
    /**
40
     *
41
     */
42
    public function initalize()
0 ignored issues
show
Coding Style introduced by
initalize uses the super-global variable $GLOBALS 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...
Coding Style introduced by
initalize 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...
43
    {
44
        if (isset($this->backup)) {
45
            throw new BadMethodCallException('Environment already backed up, can\'t initialize any longer');
46
        }
47
48
        if (!is_array($GLOBALS['argv'])) {
49
            throw new \UnexpectedValueException('Need argv to work');
50
        }
51
52
        $basename = basename($GLOBALS['argv'][0]);
53
54
        foreach ($this->keys as $key) {
55
            $buffer = $_SERVER[$key];
56
            $this->backup[$key] = $buffer;
57
            $_SERVER[$key] = str_replace($basename, 'index.php', $buffer);
58
        }
59
    }
60
61
    public function reset()
0 ignored issues
show
Coding Style introduced by
reset 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...
62
    {
63
        if (false === isset($this->backup)) {
64
            throw new BadMethodCallException('Environment not yet backed up, initalize first, can\'t reset');
65
        }
66
67
        foreach ($this->backup as $key => $value) {
68
            $_SERVER[$key] = $value;
69
        }
70
71
        $this->backup = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $backup.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
    }
73
}
74