Request   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 192
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _meta() 0 22 2
A _dirname() 0 13 1
B _protocol() 0 15 5
B _data() 0 33 4
A _dataParts() 0 18 4
B get() 0 29 4
1
<?php
2
3
/**
4
 * Looks for request information
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   HTTP
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\http;
17
18
/**
19
 * Looks for request information
20
 *
21
 * @category  Core
22
 * @package   HTTP
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
abstract class Request
30
{
31
    /**
32
     * Get request data and store it
33
     **/
34
    private static $_request = [];
35
36
    /**
37
     * Check request content and prepare data for later usage
38
     *
39
     * @param array $server Content of predefined server data
40
     *
41
     * @return array
42
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
43
44
    private static function _meta(array $server)
45
    {
46
        $request = [];
47
48
        // Get dirname and protocol
49
        $request['dirname']  = self::_dirname($server['SCRIPT_NAME']);
50
        $request['protocol'] = self::_protocol($server);
51
52
        // Get current dns and port
53
        $request['dns']  = $server['HTTP_HOST'];
54
        $request['port'] = '';
55
56
        $port_pos = strpos($request['dns'], ':');
57
58
        if ($port_pos !== false) {
59
60
            $request['dns']  = substr($request['dns'], 0, $port_pos);
61
            $request['port'] = strstr($request['dns'], ':');
62
        }
63
64
        return $request;
65
    }
66
67
    /**
68
     * Determine request dirname and query string
69
     *
70
     * @param string $script Script name of URI
71
     *
72
     * @return string
73
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
74
75
    private static function _dirname($script)
76
    {
77
        $script = rawurlencode($script);
78
        $script = str_replace('%2F', '/', $script);
79
80
        // Parse dummy URL and get path
81
        $url           = 'http://csphere.eu' . '/' . ltrim($script, '/');
82
        $parts         = parse_url($url);
83
        $parts['path'] = str_replace('/index.php', '', $parts['path']);
84
        $parts['path'] = rtrim($parts['path'], '/') . '/';
85
86
        return $parts['path'];
87
    }
88
89
    /**
90
     * Determine request protocol
91
     *
92
     * @param array $server Content of predefined server data
93
     *
94
     * @return string
95
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
96
97
    private static function _protocol(array $server)
98
    {
99
        $protocol = 'http';
100
101
        // Protocol could be forwarded by one webserver
102
        if (isset($server['HTTPS'])
103
            && $server['HTTPS'] == 'on'
104
            || isset($server['HTTP_X_FORWARDED_PROTO'])
105
            && $server['HTTP_X_FORWARDED_PROTO'] == 'https'
106
        ) {
107
            $protocol .= 's';
108
        }
109
110
        return $protocol;
111
    }
112
113
    /**
114
     * Splits the request content
115
     *
116
     * @param array $server Content of predefined server data
117
     *
118
     * @return array
119
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
120
121
    private static function _data(array $server)
122
    {
123
        $run   = 0;
124
        $map   = $server['REQUEST_URI'];
125
        $split = explode('?', $map, 2);
126
127
        // Check if request type is pretty_url based
128
        if (isset($split[1])) {
129
130
            $map = str_replace(['&', '='], '/', $split[1]);
131
132
        } else {
133
134
            $run    = 2;
135
            $length = strlen(self::$_request['dirname']);
136
            $map    = substr($map, $length);
137
            $map    = str_replace('index.php', '', $map);
138
        }
139
140
        // Creates a key value array out of the request map
141
        $parts = explode('/', $map);
142
143
        $data = self::_dataParts($run, $parts);
144
145
        // Set plugin and action for pretty_url
146
        if ($run == 2) {
147
148
            $data['plugin'] = $parts[0];
149
            $data['action'] = isset($parts[1]) ? $parts[1] : '';
150
        }
151
152
        return $data;
153
    }
154
155
    /**
156
     * Generate an array of all data parts
157
     *
158
     * @param integer $start First part that should be used
159
     * @param array   $parts Parameters as an array
160
     *
161
     * @return array
162
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
163
164
    private static function _dataParts($start, array $parts)
165
    {
166
        $data = [];
167
168
        $parts_count = count($parts);
169
170
        // Additional key value pairs
171
        for ($i = $start; $i < $parts_count; $i+=2) {
172
173
            if (!empty($parts[$i])) {
174
175
                $data[$parts[$i]] = isset($parts[($i+1)]) ?
176
                    $parts[($i+1)] : null;
177
            }
178
        }
179
180
        return $data;
181
    }
182
183
    /**
184
     * Delivers the request content
185
     *
186
     * @param string $key Get a specific array key, e.g. dirname
187
     *
188
     * @return mixed
189
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
190
191
    public static function get($key = '')
192
    {
193
        // Check if request data is already prepared
194
        if (self::$_request == []) {
195
196
            // Fetch server data
197
            $server = \csphere\core\http\Input::getAll('server');
198
199
            // Get request information and optimize its content for later usage
200
            self::$_request = self::_meta($server);
201
202
            // Get request data
203
            self::$_request['data'] = self::_data($server);
204
        }
205
206
        // Check if a key is given
207
        if (empty($key)) {
208
209
            return self::$_request;
210
211
        } elseif (isset(self::$_request[$key])) {
212
213
            return self::$_request[$key];
214
215
        } else {
216
217
            return null;
218
        }
219
    }
220
}
221