Test Failed
Pull Request — master (#3)
by Ole
08:31
created

LegacyScriptController::runLegacyScript()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 27
cp 0
rs 9.36
c 0
b 0
f 0
cc 3
nc 1
nop 2
crap 12
1
<?php
2
3
namespace Basster\LegacyBridgeBundle\Controller;
4
5
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\HttpFoundation\StreamedResponse;
8
9
/**
10
 * Class LegacyScriptController
11
 *
12
 * @package Basster\LegacyBridgeBundle\Controller
13
 */
14
class LegacyScriptController implements ContainerAwareInterface
15
{
16
    /**
17
     * @var ContainerInterface
18
     */
19
    private $container;
20
21
    /** @var  string */
22
    private $prependScript;
23
24
    /** @var  string */
25
    private $appendScript;
26
27
    /**
28
     * @param $requestPath
29
     * @param $legacyScript
30
     *
31
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
32
     */
33
    public function runLegacyScript($requestPath, $legacyScript)
0 ignored issues
show
Coding Style introduced by
runLegacyScript 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...
34
    {
35
        $container = $this->container;
36
        $prepend   = $this->prependScript;
37
        $append    = $this->appendScript;
38
39
        $requireLegacyScript = function () use (
40
          $requestPath,
41
          $legacyScript,
42
          $container,
43
          $prepend,
44
          $append
45
        ) {
46
            $_SERVER['PHP_SELF']          = $requestPath;
47
            $_SERVER['SCRIPT_NAME']       = $requestPath;
48
            $_SERVER['SCRIPT_FILENAME']   = $legacyScript;
49
            $_SERVER['SYMFONY_CONTAINER'] = $container;
50
            chdir(dirname($legacyScript));
51
52
            if ($prepend) {
53
                /** @noinspection PhpIncludeInspection */
54
                require $prepend;
55
            }
56
57
            /** @noinspection PhpIncludeInspection */
58
            require $legacyScript;
59
60
            if ($append) {
61
                /** @noinspection PhpIncludeInspection */
62
                require $append;
63
            }
64
        };
65
66
        return StreamedResponse::create($requireLegacyScript);
67
    }
68
69
    /**
70
     * @param string|null $script
71
     */
72
    public function setPrependScript($script = null)
73
    {
74
        $this->prependScript = $script;
75
    }
76
77
    /**
78
     * @param string|null $script
79
     */
80
    public function setAppendScript($script = null)
81
    {
82
        $this->appendScript = $script;
83
    }
84
85
    /** {@inheritdoc} */
86
    public function setContainer(ContainerInterface $container = null)
87
    {
88
        $this->container = $container;
89
    }
90
}
91