Completed
Push — master ( 69e974...6c5ab2 )
by Bohuslav
02:42
created

Environment::getPort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
namespace Kambo\Http\Message\Environment;
3
4
// \Spl
5
use InvalidArgumentException;
6
7
// \Http\Message
8
use Kambo\Http\Message\Environment\Interfaces\Environment as EnvironmentInterface;
9
10
/**
11
 * Contains information about server and HTTP request - headers, cookies, files and body data.
12
 *
13
 * @package Kambo\Http\Message\Environment\Interfaces\Environment
14
 * @author  Bohuslav Simek <[email protected]>
15
 * @license MIT
16
 */
17
class Environment implements EnvironmentInterface
18
{
19
    /**
20
     * Server environment data, must have same structure as $_SERVER superglobal.
21
     *
22
     * @var array
23
     */
24
    private $server;
25
26
    /**
27
     * Raw data from the request body.
28
     *
29
     * @var resource
30
     */
31
    private $body;
32
33
    /**
34
     * An associative array constructed from cookies, must have same structure as $_COOKIE.
35
     *
36
     * @var array
37
     */
38
    private $cookies;
39
40
    /**
41
     * An associative array of uploaded items, must have same structure as $_FILES.
42
     *
43
     * @var array
44
     */
45
    private $files;
46
47
    /**
48
     * An associative array of variables passed to the current script via the HTTP POST method.
49
     *
50
     * @var array
51
     */
52
    private $post;
53
54
    /**
55
     * Constructor
56
     *
57
     * @param array    $server An associative array containing information such as headers, paths, 
58
     *                         and script locations. Must have same structure as $_SERVER.
59
     * @param resource $body   Raw data from the request body.
60
     * @param array    $post   An associative array of variables passed to the current script via the HTTP POST method.
61
     * @param array    $cookie An associative array constructed from cookies, must have same structure as $_COOKIE. 
62
     * @param array    $files  An associative array of uploaded items, must have same structure as $_FILES.
63
     *
64
     */
65 24
    public function __construct(array $server, $body, $post = [], $cookie = [], $files = [])
66
    {
67 24
        if (!is_resource($body)) {
68 1
            throw new InvalidArgumentException('Provided body must be of type resource.');
69
        }
70
71 23
        $this->server  = $server;
72 23
        $this->body    = $body;
73 23
        $this->post    = $post;
74 23
        $this->cookies = $cookie;
75 23
        $this->files   = $files;
76 23
    }
77
78
    /**
79
     * Get query string
80
     *
81
     * @return string|null query string
82
     */
83 2
    public function getQueryString()
84
    {
85 2
        return isset($this->server['QUERY_STRING']) ? $this->server['QUERY_STRING'] : null;
86
    }
87
88
    /**
89
     * Get request method
90
     *
91
     * @return string|null
92
     */
93 2
    public function getRequestMethod()
94
    {
95 2
        return isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : null;
96
    }
97
98
    /**
99
     * Get request uri
100
     *
101
     * @return string|null
102
     */
103 2
    public function getRequestUri()
104
    {
105 2
        return isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : null;
106
    }
107
    /**
108
     * Get request scheme
109
     *
110
     * @return string|null
111
     */
112 2
    public function getRequestScheme()
113
    {
114 2
        return isset($this->server['REQUEST_SCHEME']) ? $this->server['REQUEST_SCHEME'] : null;
115
    }
116
117
    /**
118
     * Get host
119
     *
120
     * @return string|null
121
     */
122 2
    public function getHost()
123
    {
124 2
        return isset($this->server['HTTP_HOST']) ? $this->server['HTTP_HOST'] : null;
125
    }
126
127
    /**
128
     * Get port
129
     *
130
     * @return int|null
131
     */
132 2
    public function getPort()
133
    {
134 2
        return isset($this->server['SERVER_PORT']) ? $this->server['SERVER_PORT'] : null;
135
    }
136
137
    /**
138
     * Get protocol version
139
     *
140
     * @return string|null
141
     */
142 2
    public function getProtocolVersion()
143
    {
144 2
        $version = null;
145 2
        if (isset($this->server['SERVER_PROTOCOL'])) {
146 1
            $protocol = $this->server['SERVER_PROTOCOL'];
147 1
            list(,$version) = explode("/", $protocol);
148 1
        }
149
150 2
        return $version;
151
    }
152
153
    /**
154
     * Get auth user
155
     *
156
     * @return string|null
157
     */
158 2
    public function getAuthUser()
159
    {
160 2
        if (isset($this->server['PHP_AUTH_USER'])) {
161 1
            return $this->server['PHP_AUTH_USER'];
162
        }
163
164 1
        return null;
165
    }
166
167
    /**
168
     * Get auth password
169
     *
170
     * @return string|null
171
     */
172 2
    public function getAuthPassword()
173
    {
174 2
        if (isset($this->server['PHP_AUTH_PW'])) {
175 1
            return $this->server['PHP_AUTH_PW'];
176
        }
177
178 1
        return null;
179
    }
180
181
    /**
182
     * Get body
183
     *
184
     * @return resource
185
     */
186 1
    public function getBody()
187
    {
188 1
        return $this->body;
189
    }
190
191
    /**
192
     * Get body
193
     *
194
     * @return resource
195
     */
196 1
    public function getPost()
197
    {
198 1
        return $this->post;
199
    }
200
201
    /**
202
     * Get cookies
203
     *
204
     * @return array
205
     */
206 1
    public function getCookies()
207
    {
208 1
        return $this->cookies;
209
    }
210
211
    /**
212
     * Get files
213
     *
214
     * @return array
215
     */
216 1
    public function getFiles()
217
    {
218 1
        return $this->files;
219
    }
220
221
    /**
222
     * Get server
223
     *
224
     * @return array
225
     */
226 1
    public function getServer()
227
    {
228 1
        return $this->server;
229
    }
230
}
231