LegacyController   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 79.66%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 8
dl 0
loc 125
ccs 47
cts 59
cp 0.7966
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B indexAction() 0 39 9
B setGetVariables() 0 36 10
B runScript() 0 26 6
1
<?php
2
3
/**
4
 * @author Matthias Glaub <[email protected]>
5
 * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
6
 */
7
8
namespace MaglLegacyApplication\Controller;
9
10
use Laminas\Http\Response;
11
use Laminas\Mvc\Controller\AbstractActionController;
12
use Laminas\View\Model\ViewModel;
13
use MaglLegacyApplication\Application\MaglLegacy;
14
use MaglLegacyApplication\Options\LegacyControllerOptions;
15
16
class LegacyController extends AbstractActionController
17
{
18
19
    /**
20
     *
21
     * @var LegacyControllerOptions
22
     */
23
    private $options;
24
25
    /**
26
     *
27
     * @var MaglLegacy
28
     */
29
    private $legacy;
30
31 8
    public function __construct(LegacyControllerOptions $options, MaglLegacy $legacy)
32
    {
33 8
        $this->options = $options;
34 8
        $this->legacy = $legacy;
35 8
    }
36
37 7
    public function indexAction()
38
    {
39 7
        $docRoots = $this->options->getDocRoots();
40 7
        foreach ($docRoots as $key => $docRoot) {
41 7
            $docRoots[$key] = rtrim(getcwd() . '/' . $docRoot);
42
        }
43
44 7
        $scriptName = $this->params('script');
45
46 7
        if (empty($scriptName)) {
47
            $path = $this->params(('path')) ? $this->params('path') : '';
48
            foreach ($this->options->getIndexFiles() as $indexFile) {
49
                foreach ($docRoots as $docRoot) {
50
                    if (is_file($docRoot . '/' . $path . $indexFile)) {
51
                        $this->legacy->setLegacyScriptName($path . $indexFile);
52
                        return $this->runScript($docRoot . '/' . $path . $indexFile);
53
                    }
54
                }
55
            }
56
        }
57
58 7
        $scriptUri = '/' . ltrim($scriptName, '/'); // force leading '/'
59 7
        foreach ($docRoots as $docRoot) {
60 7
            $legacyScriptFilename = $docRoot . $scriptUri;
61 7
            if (is_file($legacyScriptFilename)) {
62
                //inform the application about the used script
63 5
                $this->legacy->setLegacyScriptName($scriptUri);
64
65
                //inject get and request variables
66 5
                $this->setGetVariables();
67
68 5
                return $this->runScript($legacyScriptFilename);
69
            }
70
        }
71
72
        /** @var Response $response */
73 2
        $response = $this->getResponse();
74 2
        $response->setStatusCode(404);
75 2
    }
76
77 5
    private function setGetVariables()
78
    {
79 5
        $globals_options = $this->options->getGlobals();
80
81
        // if we should not set any global vars, we can return safely
82 5
        if (!$globals_options['get'] && !$globals_options['request']) {
83
            return;
84
        }
85
86
        // check if $_GET is used at all (ini - variables_order ?)
87
        // check if $_GET is written to $_REQUEST (ini - variables_order / request_order)
88
        // depending on request_order, check if $_REQUEST is already written and decide if we are allowed to override
89
90 5
        $request_order = ini_get('request_order');
91
92 5
        if ($request_order === false) {
93
            $request_order = ini_get('variables_order');
94
        }
95
96 5
        $get_prio = stripos($request_order, 'g');
97 5
        $post_prio = stripos($request_order, 'p');
98
99 5
        $forceOverrideRequest = $get_prio > $post_prio;
100
101 5
        $routeParams = $this->getEvent()->getRouteMatch()->getParams();
102
103 5
        foreach ($routeParams as $paramName => $paramValue) {
104 5
            if ($globals_options['get'] && !isset($_GET[$paramName])) {
105 5
                $_GET[$paramName] = $paramValue;
106
            }
107
108 5
            if ($globals_options['request'] && ($forceOverrideRequest || !isset($_REQUEST[$paramName]))) {
109 5
                $_REQUEST[$paramName] = $paramValue;
110
            }
111
        }
112 5
    }
113
114 5
    private function runScript($scriptFileName)
115
    {
116 5
        $this->legacy->setLegacyScriptFilename($scriptFileName);
117
118 5
        ob_start();
119 5
        $result = include $scriptFileName;
120 5
        $output = ob_get_clean();
121
122 5
        if ($result instanceof ViewModel || $result instanceof Response) {
123 2
            if ($this->options->getPrependOutputBufferToResponse()) {
124
                echo $output;
125
            }
126 2
            return $result;
127
        }
128
129 3
        $result = $this->getEventManager()->trigger(MaglLegacy::EVENT_SHORT_CIRCUIT_RESPONSE, $this);
130 3
        if ($result->stopped()) {
131
            if ($this->options->getPrependOutputBufferToResponse()) {
132
                $result->last()->setContent($output . $result->last()->getContent());
133
            }
134
            return $result->last();
135
        }
136
137 3
        $this->getResponse()->setContent($output);
138 3
        return $this->getResponse();
139
    }
140
}
141