Completed
Push — master ( d9f7fe...0d6725 )
by Igor
02:53
created

Request::__construct()   C

Complexity

Conditions 11
Paths 54

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 6.7478
c 0
b 0
f 0
cc 11
nc 54
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
 */
5
namespace Pivasic\Core;
6
7
/**
8
 * Represents a HTTP request.
9
 *
10
 * Class Request
11
 * @package Pivasic\Core
12
 */
13
class Request
14
{
15
    /**
16
     * Request constructor.
17
     *
18
     * @param array $languageList
19
     * @param array $packageList
20
     * @param null|string $url
21
     */
22
    public function __construct(array $languageList = [], array $packageList = [], $url = null)
23
    {
24
        if (null === $url) {
25
            $url = filter_input_array(INPUT_SERVER)['REQUEST_URI'] ?? '';
26
        }
27
        $this->method = 'GET';
28
        $this->defaultLanguage = $languageList[0] ?? '';
29
        $this->language = '';
30
        $this->package = 'Original';
31
        $this->route = 'index';
32
33
        $url = explode('?', $url);
34
        $url = trim(trim($url[0]), '/');
35
        if ('' != $url) {
36
            $partList = explode('/', $url);
37
38
            /**
39
             * Check language.
40
             */
41
            if (false !== ($key = array_search($partList[0], $languageList))) {
42
                unset($partList[0]);
43
                $partList = array_values($partList);
44
                $this->language = $languageList[$key];
45
            }
46
47
            /**
48
             * Check package.
49
             */
50
            if (isset($partList[0]) && false !== ($key = array_search(ucfirst($partList[0]), $packageList))) {
51
                unset($partList[0]);
52
                $partList = array_values($partList);
53
                $this->package = $packageList[$key];
54
            }
55
56
            /**
57
             * Get route.
58
             */
59
            if (isset($partList[0])) {
60
                $this->route = implode('/', $partList);
61
                $this->route = trim(str_replace('.', '_', $this->route), '/');
62
            }
63
        }
64
65
        /**
66
         * Request URL.
67
         */
68
        $this->url = '/' . $url;
69
70
        /**
71
         * Get method.
72
         */
73
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])) {
74
            $this->method = 'AJAX';
75
        } else {
76
            if (isset($_SERVER['REQUEST_METHOD']) && in_array($_SERVER['REQUEST_METHOD'], ['GET', 'POST', 'HEAD', 'PUT', 'DELETE'])) {
77
                $this->method = $_SERVER['REQUEST_METHOD'];
78
            }
79
        }
80
    }
81
82
    /**
83
     * Path without an optional query.
84
     *
85
     * @return string
86
     */
87
    public function url()
88
    {
89
        return $this->url;
90
    }
91
92
    /**
93
     * Name of an app package.
94
     *
95
     * @return string
96
     */
97
    public function package()
98
    {
99
        return $this->package;
100
    }
101
102
    /**
103
     * URL path without language code, package name and an optional query parameters.
104
     *
105
     * @return string
106
     */
107
    public function route()
108
    {
109
        return $this->route;
110
    }
111
112
    /**
113
     * Request method.
114
     *
115
     * @return string
116
     */
117
    public function method()
118
    {
119
        return $this->method;
120
    }
121
122
    /**
123
     * A language code.
124
     *
125
     * @return string
126
     */
127
    public function language()
128
    {
129
        return $this->language;
130
    }
131
132
    /**
133
     * A default language code.
134
     *
135
     * @return string
136
     */
137
    public function defaultLanguage()
138
    {
139
        return $this->defaultLanguage;
140
    }
141
142
    private $url;
143
    private $package;
144
    private $route;
145
    private $method;
146
    private $language;
147
    private $defaultLanguage;
148
}