Completed
Push — master ( 9e8dbe...382c30 )
by Joao
06:47
created

src/HttpRequest.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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
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
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
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
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
        $headers = [
148
            'HTTP_CLIENT_IP',
149
            'HTTP_X_FORWARDED_FOR',
150
            'HTTP_X_FORWARDED',
151
            'HTTP_FORWARDED_FOR',
152
            'HTTP_FORWARDED',
153
            'REMOTE_ADDR',
154
        ];
155
        foreach ($headers as $header) {
156
            if ($this->server($header) !== false) {
157
                return $this->server($header);
158
            }
159
        }
160
161
        return 'UNKNOWN';
162
    }
163
164
    public function getServerName()
165
    {
166
        $headers = [
167
            'SERVER_NAME',
168
            'HTTP_HOST',
169
        ];
170
        foreach ($headers as $header) {
171
            if ($this->server($header) !== false) {
172
                return $this->server('SERVER_NAME');
173
            }
174
        }
175
        return $this->server('SERVER_ADDR');
176
    }
177
178
    /**
179
     * Use this method to get the SERVER NAME.
180
     * @param bool $port
181
     * @param bool $protocol
182
     * @return string
183
     */
184
    public function getRequestServer($port = false, $protocol = false)
185
    {
186
        $servername = $this->getServerName();
187
188
        if ($port && $this->server('SERVER_PORT' !== false)) {
0 ignored issues
show
'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...
189
            $servername .= ':' . $this->server('SERVER_PORT');
190
        }
191
192
        if ($protocol) {
193
            $servername = (
194
                ($this->server('HTTPS') !== 'off'
195
                    || $this->server('SERVER_PORT') == 443) ? "https://" : "http://") . $servername
196
            ;
197
        }
198
199
        return $servername;
200
    }
201
202
    private $uploadedFiles;
203
204
    /**
205
     * @return \ByJG\RestServer\UploadedFiles
206
     */
207
    public function uploadedFiles()
208
    {
209
        if (!isset($this->uploadedFiles)) {
210
            $this->uploadedFiles = new UploadedFiles();
211
        }
212
        return $this->uploadedFiles;
213
    }
214
}
215