Controller::parseRequestAction()   B
last analyzed

Complexity

Conditions 7
Paths 20

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 29
nc 20
nop 0
dl 0
loc 52
rs 7.2396
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Find and execute the best solution for a request
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Router
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\router;
17
18
/**
19
 * Class controller knows best what to do with the request
20
 *
21
 * @category  Core
22
 * @package   Router
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Controller
30
{
31
    /**
32
     * Store plugin with target to use
33
     **/
34
    private $_target = '';
35
36
    /**
37
     * Store if the target is a box
38
     **/
39
    private $_box = false;
40
41
    /**
42
     * Store if the request expects a json object
43
     **/
44
    private $_xhr = false;
45
46
    /**
47
     * Creates a new router for executing a destination
48
     *
49
     * @param boolean $search Set this to true to auto discover the target
50
     *
51
     * @return \csphere\core\router\Controller
52
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
53
54
    public function __construct($search = false)
55
    {
56
        // Check for xhr mode that needs a json object
57
        $xhr = \csphere\core\http\Input::get('get', 'xhr');
58
59
        if ($xhr == 1) {
60
61
            $this->_xhr = true;
62
        }
63
64
        // Check if search is enabled
65
        if ($search == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
66
67
            $this->_search();
68
        }
69
    }
70
71
    /**
72
     * Execute the target plugin target
73
     *
74
     * @param boolean $skip Whether to skip target execution
75
     *
76
     * @return void
77
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
78
79
    public function execute($skip = false)
80
    {
81
        // Check if response was already send
82
        $status = \csphere\core\http\Response::status();
83
84
        if ($status === false) {
85
86
            // If skip is true the view was already filled with content
87
            if ($skip === false) {
88
89
                \csphere\core\router\Sandbox::light($this->_target);
90
            }
91
92
            // Get content from view
93
            $loader  = \csphere\core\service\Locator::get();
94
            $view    = $loader->load('view');
95
            $content = $view->assemble($this->_xhr, $this->_box);
96
97
            // Tell response whether to use compression
98
            $zlib = false;
99
100
            if ($skip == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
101
102
                $zlib = (boolean)$view->getOption('zlib');
103
            }
104
105
            \csphere\core\http\Response::compression($zlib);
106
107
            // Send content to response
108
            \csphere\core\http\Response::send($content);
109
        }
110
    }
111
112
    /**
113
     * Gets a target action or box of a plugin
114
     *
115
     * @param string $plugin Plugin name
116
     * @param string $action Action name
117
     *
118
     * @return string
119
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
120
121
    public function target($plugin, $action)
122
    {
123
        $checks = new \csphere\core\plugins\Checks($plugin);
124
        $checks->setRoute($action);
125
126
        // Keep initial action while allowing changes to it
127
        $new_action = $action;
128
129
        // Fallback if action does not exist
130
        if ($checks->existance() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
131
132
            $new_action = 'dispatch';
133
            $checks->setRoute($new_action);
134
135
            // Reset fallback if no plugin dispatcher was found
136
            if ($checks->existance() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
137
138
                $new_action = $action;
139
                $checks->setRoute($new_action);
140
            }
141
        }
142
143
        // Inform template engine about the target
144
        \csphere\core\template\Hooks::route($plugin, $new_action);
145
146
        $target = $checks->result();
147
148
        return $target;
149
    }
150
151
    /**
152
     * Gets a target action or box of a plugin
153
     *
154
     * @return string
155
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
156
157
    private function _search()
158
    {
159
160
        $request=self::parseRequestAction();
161
162
        $plugin=$request['plugin'];
163
        $action=$request['action'];
164
        $box=$request['box'];
165
166
        // Fetch defaults if no plugin is given
167
        if (!empty($box)) {
168
169
            $this->_box = true;
170
171
            // Special case for box requests
172
            $checks = new \csphere\core\plugins\Checks($plugin);
173
            $checks->setRoute($box, true);
0 ignored issues
show
Unused Code introduced by
The call to Checks::setRoute() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
174
            $this->_target = $checks->result();
175
176
        } else {
177
178
            $this->_target = $this->target($plugin, $action);
179
        }
180
    }
181
182
    /**
183
     * Get the current requested plugin, action and box
184
     *
185
     * @return array
186
     */
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
187
188
    public static function parseRequestAction()
189
    {
190
        $box    = \csphere\core\http\Input::get('get', 'box');
191
        $plugin = \csphere\core\http\Input::get('get', 'plugin');
192
        $action = \csphere\core\http\Input::get('get', 'action');
193
194
        $request=[
195
            'plugin'=>'',
196
            'action'=>'',
197
            'box'=>''
198
        ];
199
200
        // Read config file
201
        $conf   = new \csphere\core\init\Config();
202
        $config = $conf->get();
203
204
        // Check for favicon requests
205
        if ($plugin == 'favicon.ico') {
206
            $plugin = '';
207
        }
208
209
        if (empty($config['default']['action'])) {
210
            $config['default']['action']="list";
211
        }
212
        
213
        if (empty($plugin)) {
214
215
            if (empty($config['default']['plugin'])) {
216
                $config['default']['plugin']="default";
217
            }
218
219
            $request['plugin']=$config['default']['plugin'];
220
221
            $request['action']=$config['default']['action'];
222
223
224
        } elseif (!empty($box)) {
225
            $request['plugin']=$plugin;
226
            $request['box']=$box;
227
        } else {
228
229
            $request['plugin']=$plugin;
230
231
            if (empty($action)) {
232
                $request['action']=$config['default']['action'];
233
            } else {
234
                $request['action']=$action;
235
            }
236
        }
237
238
        return $request;
239
    }
240
}
241