Completed
Push — master ( fa6e5d...72db9f )
by Igor
02:28
created

Request::__construct()   C

Complexity

Conditions 13
Paths 102

Size

Total Lines 65
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 65
rs 5.9088
cc 13
eloc 32
nc 102
nop 3

How to fix   Long Method    Complexity   

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
 * @license MIT
4
 * @author Igor Sorokin <[email protected]>
5
 */
6
namespace Dspbee\Core;
7
8
/**
9
 * Represents a HTTP request.
10
 *
11
 * Class Request
12
 * @package Dspbee\Core
13
 */
14
class Request
15
{
16
    /**
17
     * Request constructor.
18
     *
19
     * @param array $languageList
20
     * @param array $packageList
21
     * @param null $url
22
     */
23
    public function __construct(array $languageList = [], array $packageList = [], $url = null)
24
    {
25
        if (null === $url) {
26
            $url = $_SERVER['REQUEST_URI'] ?? '';
27
        }
28
29
        $this->method = 'GET';
30
31
        $this->languageDefault = key($languageList);
32
        $this->languageCode = '';
33
        $this->languageName = '';
34
        $this->package = 'Original';
35
        $this->packageRoute = $packageList['Original'] ?? false;
36
        $this->route = 'index';
37
38
        $url = explode('?', $url);
39
        $url = $url[0];
40
        $url = trim(trim($url), '/');
41
        if ('' !== $url) {
42
            $partList = explode('/', $url);
43
            /**
44
             * Delete front controller.
45
             */
46
            if (false !== strpos($partList[0], '.php')) {
47
                array_shift($partList);
48
            }
49
            /**
50
             * Check language.
51
             */
52
            if (isset($partList[0]) && isset($languageList[$partList[0]])) {
53
                $this->languageCode = array_shift($partList);
54
                $this->languageName = $languageList[$this->languageCode];
55
            }
56
            /**
57
             * Check package.
58
             */
59
            if (isset($partList[0]) && isset($packageList[ucfirst($partList[0])])) {
60
                $this->package = ucfirst(array_shift($partList));
61
                $this->packageRoute = $packageList[$this->package];
62
            }
63
            /**
64
             * Get route.
65
             */
66
            if (count($partList)) {
67
                $this->route = implode('/', $partList);
68
                $this->route = trim(str_replace('.', '_', $this->route), '/');
69
            }
70
        }
71
72
        /**
73
         * Request URL.
74
         */
75
        $this->url = '/' . $url;
76
77
        /**
78
         * Get method.
79
         */
80
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])) {
81
            $this->method = 'AJAX';
82
        } else {
83
            if (isset($_SERVER['REQUEST_METHOD'])) {
84
                $this->method = $_SERVER['REQUEST_METHOD'];
85
            }
86
        }
87
    }
88
89
    /**
90
     * Path without an optional query.
91
     *
92
     * @return string
93
     */
94
    public function url()
95
    {
96
        return $this->url;
97
    }
98
99
    /**
100
     * Create absolute url path or full uri from route.
101
     *
102
     * @param string $route
103
     * @param bool $domain
104
     *
105
     * @return string
106
     */
107
    public function makeUrl($route = '', $domain = false)
108
    {
109
        $controller = '';
110
        if (false !== strpos($this->url, '.php')) {
111
            $controller = explode('.php', $this->url);
112
            $controller = ltrim($controller[0], '/') . '.php';
113
        }
114
        $route = trim($route, '/');
115
        if ('Original' == $this->package) {
116
            if ($this->languageCode != $this->languageDefault) {
117
                $url = trim('/' . $this->languageCode . '/' . $route, '/');
118
            } else {
119
                $url = trim('/' . $route, '/');
120
            }
121
        } else {
122
            if ($this->languageCode != $this->languageDefault) {
123
                $url = trim('/' . $this->languageCode . '/' . lcfirst($this->package) . '/' . $route, '/');
124
            } else {
125
                $url = trim('/' . lcfirst($this->package) . '/' . $route, '/');
126
            }
127
        }
128
        if ($domain) {
129
            $host = '';
130
            if (isset($_SERVER['HTTP_HOST'])) {
131
                $host = $_SERVER['HTTP_HOST'];
132
            } else if (isset($_SERVER['SERVER_NAME'])) {
133
                $host = $_SERVER['SERVER_NAME'];
134
            }
135
            if (
136
                (isset($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) ||
137
                (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT'])
138
            ) {
139 View Code Duplication
                if (empty($controller)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
                    $url = 'https://' . $host . '/' . $url;
141
                } else {
142
                    $url = 'https://' . $host . '/' . $controller . '/' . $url;
143
                }
144 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
                if (empty($controller)) {
146
                    $url = 'http://' . $host . '/' . $url;
147
                } else {
148
                    $url = 'http://' . $host . '/' . $controller . '/' . $url;
149
                }
150
            }
151
        } else {
152
            if (empty($controller)) {
153
                $url = '/' . $url;
154
            } else {
155
                $url = '/' . $controller . '/' . $url;
156
            }
157
        }
158
159
        return $url;
160
    }
161
162
163
    /**
164
     * Request method.
165
     *
166
     * @return string
167
     */
168
    public function method()
169
    {
170
        return $this->method;
171
    }
172
173
    /**
174
     * A default language code.
175
     *
176
     * @return string
177
     */
178
    public function languageDefault()
179
    {
180
        return $this->languageDefault;
181
    }
182
183
    /**
184
     * A language code.
185
     *
186
     * @return string
187
     */
188
    public function languageCode()
189
    {
190
        return $this->languageCode;
191
    }
192
193
    /**
194
     * A language name associated with code.
195
     *
196
     * @return string
197
     */
198
    public function languageName()
199
    {
200
        return $this->languageName;
201
    }
202
203
    /**
204
     * Name of an app package.
205
     *
206
     * @return string
207
     */
208
    public function package()
209
    {
210
        return $this->package;
211
    }
212
213
    /**
214
     * Custom routing class or false.
215
     *
216
     * @return string|bool
217
     */
218
    public function packageRoute()
219
    {
220
        return $this->packageRoute;
221
    }
222
223
    /**
224
     * URL path without front controller, language code, package name and an optional query.
225
     *
226
     * @return string
227
     */
228
    public function route()
229
    {
230
        return $this->route;
231
    }
232
233
    private $method;
234
235
    private $url;
236
    private $languageDefault;
237
    private $languageCode;
238
    private $languageName;
239
    private $package;
240
    private $packageRoute;
241
    private $route;
242
}