Completed
Pull Request — master (#1)
by Joao
04:49
created

HttpRequest::getRequestIp()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.2222
cc 7
eloc 16
nc 7
nop 0
1
<?php
2
3
namespace ByJG\RestServer;
4
5
class HttpRequest
6
{
7
8
    protected $get;
9
    protected $post;
10
    protected $server;
11
    protected $session;
12
    protected $cookie;
13
    protected $request;
14
15
    /**
16
     *
17
     * @param array $get
18
     * @param array $post
19
     * @param array $server
20
     * @param array $session
21
     * @param array $cookie
22
     */
23
    public function __construct($get, $post, $server, $session, $cookie)
24
    {
25
        $this->get = $get;
26
        $this->post = $post;
27
        $this->server = $server;
28
        $this->session = $session;
29
        $this->cookie = $cookie;
30
31
        $this->request = array_merge($get, $post, $server, $session, $cookie);
32
    }
33
34
    /**
35
     * Get a parameter passed by GET (the same as $_GET). If not found return false.
36
     *
37
     * @param string $value
38
     * @return string|boolean
39
     */
40 View Code Duplication
    public function get($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        if (!isset($this->get[$value])) {
43
            return false;
44
        } else {
45
            return $this->get[$value];
46
        }
47
    }
48
49
    /**
50
     * Get a parameter passed by POST (the same as $_POST). If not found return false.
51
     *
52
     * @param string $value
53
     * @return string|boolean
54
     */
55 View Code Duplication
    public function post($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
56
    {
57
        if (!isset($this->post[$value])) {
58
            return false;
59
        } else {
60
            return $this->post[$value];
61
        }
62
    }
63
64
    /**
65
     * Get the parameters sent by server (the same as $_SERVER). If not found return false.
66
     *
67
     * @param string $value
68
     * @return string|boolean
69
     */
70 View Code Duplication
    public function server($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
71
    {
72
        if (!isset($this->server[$value])) {
73
            return false;
74
        } else {
75
            return $this->server[$value];
76
        }
77
    }
78
79
    /**
80
     * Get a server session value(the same as $_SESSION). If not found return false.
81
     *
82
     * @param string $value
83
     * @return string|boolean
84
     */
85 View Code Duplication
    public function session($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
86
    {
87
        if (!isset($this->session[$value])) {
88
            return false;
89
        } else {
90
            return $this->session[$value];
91
        }
92
    }
93
94
    /**
95
     * Get the cookie sent by the client (the same as $_COOKIE). If not found return false.
96
     *
97
     * @param string $value
98
     * @return string|boolean
99
     */
100 View Code Duplication
    public function cookie($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
101
    {
102
        if (!isset($this->cookie[$value])) {
103
            return false;
104
        } else {
105
            return $this->cookie[$value];
106
        }
107
    }
108
109
    /**
110
     * Get a value from any of get, post, server, cookie or session. If not found return false.
111
     *
112
     * @param string $value
113
     * @return string|boolean
114
     */
115 View Code Duplication
    public function request($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
116
    {
117
        if (!isset($this->request[$value])) {
118
            return false;
119
        } else {
120
            return $this->request[$value];
121
        }
122
    }
123
124
    private $payload;
125
126
    /**
127
     * Get the payload passed during the request(the same as php://input). If not found return empty.
128
     *
129
     * @return string
130
     */
131
    public function payload()
132
    {
133
        if (is_null($this->payload)) {
134
            $this->payload = file_get_contents("php://input");
135
        }
136
137
        return $this->payload;
138
    }
139
140
    /**
141
     * Use this method to get the CLIENT REQUEST IP.
142
     * Note that if you behing a Proxy, the variable REMOTE_ADDR will always have the same IP
143
     * @return string
144
     */
145
    public function getRequestIp()
146
    {
147
        if ($this->server('HTTP_CLIENT_IP') !== false) {
148
            $ipaddress = $this->server('HTTP_CLIENT_IP');
149
        } else if ($this->server('HTTP_X_FORWARDED_FOR') !== false) {
150
            $ipaddress = $this->server('HTTP_X_FORWARDED_FOR');
151
        } else if ($this->server('HTTP_X_FORWARDED') !== false) {
152
            $ipaddress = $this->server('HTTP_X_FORWARDED');
153
        } else if ($this->server('HTTP_FORWARDED_FOR') !== false) {
154
            $ipaddress = $this->server('HTTP_FORWARDED_FOR');
155
        } else if ($this->server('HTTP_FORWARDED') !== false) {
156
            $ipaddress = $this->server('HTTP_FORWARDED');
157
        } else if ($this->server('REMOTE_ADDR') !== false) {
158
            $ipaddress = $this->server('REMOTE_ADDR');
159
        } else {
160
            $ipaddress = 'UNKNOWN';
161
        }
162
163
        return $ipaddress;
164
    }
165
166
    /**
167
     * Use this method to get the SERVER NAME.
168
     * @param bool $port
169
     * @param bool $protocol
170
     * @return string
171
     */
172
    public function getRequestServer($port = false, $protocol = false)
173
    {
174
        if ($this->server('SERVER_NAME') !== false) {
175
            $servername = $this->server('SERVER_NAME');
176
        } else if ($this->server('HTTP_HOST' !== false)) {
0 ignored issues
show
Documentation introduced by
'HTTP_HOST' !== false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177
            $servername = $this->server('HTTP_HOST');
178
        } else {
179
            $servername = $this->server('SERVER_ADDR');
180
        }
181
182
        if ($port && $this->server('SERVER_PORT' !== false)) {
0 ignored issues
show
Documentation introduced by
'SERVER_PORT' !== false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
            $servername .= ':' . $this->server('SERVER_PORT');
184
        }
185
186
        if ($protocol) {
187
            $servername = (($this->server('HTTPS') !== 'off' || $this->server('SERVER_PORT') == 443) ? "https://" : "http://") . $servername;
188
        }
189
190
        return $servername;
191
    }
192
193
    private $uploadedFiles;
194
195
    /**
196
     * @return \ByJG\RestServer\UploadedFiles
197
     */
198
    public function uploadedFiles()
199
    {
200
        if (!isset($this->uploadedFiles)) {
201
            $this->uploadedFiles = new UploadedFiles();
202
        }
203
        return $this->uploadedFiles;
204
    }
205
}
206