Completed
Push — master ( f85b73...7f85ed )
by Bohuslav
02:46
created

Environment   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 195
ccs 43
cts 43
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getQueryString() 0 4 2
A getRequestMethod() 0 4 2
A getRequestUri() 0 4 2
A getRequestScheme() 0 4 2
A getHost() 0 4 2
A getPort() 0 4 2
A getProtocolVersion() 0 10 2
A getAuthUser() 0 8 2
A getAuthPassword() 0 8 2
A getBody() 0 4 1
A getCookies() 0 4 1
A getFiles() 0 4 1
A getServer() 0 4 1
1
<?php
2
namespace Kambo\HttpMessage\Environment;
3
4
// \Spl
5
use InvalidArgumentException;
6
7
// \HttpMessage
8
use Kambo\HttpMessage\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\HttpMessage\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
     * Constructor
49
     *
50
     * @param array    $server An associative array containing information such as headers, paths, 
51
     *                         and script locations. Must have same structure as $_SERVER.
52
     * @param resource $body   Raw data from the request body.
53
     * @param array    $cookie An associative array constructed from cookies, must have same structure as $_COOKIE. 
54
     * @param array    $files  An associative array of uploaded items, must have same structure as $_FILES.
55
     *
56
     */
57 23
    public function __construct(array $server, $body, $cookie = [], $files = [])
58
    {
59 23
        if (!is_resource($body)) {
60 1
            throw new InvalidArgumentException('Provided body must be of type resource.');
61
        }
62
63 22
        $this->server  = $server;
64 22
        $this->body    = $body;
65 22
        $this->cookies = $cookie;
66 22
        $this->files   = $files;
67 22
    }
68
69
    /**
70
     * Get query string
71
     *
72
     * @return string|null query string
73
     */
74 2
    public function getQueryString()
75
    {
76 2
        return isset($this->server['QUERY_STRING']) ? $this->server['QUERY_STRING'] : null;
77
    }
78
79
    /**
80
     * Get request method
81
     *
82
     * @return string|null
83
     */
84 2
    public function getRequestMethod()
85
    {
86 2
        return isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : null;
87
    }
88
89
    /**
90
     * Get request uri
91
     *
92
     * @return string|null
93
     */
94 2
    public function getRequestUri()
95
    {
96 2
        return isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : null;
97
    }
98
    /**
99
     * Get request scheme
100
     *
101
     * @return string|null
102
     */
103 2
    public function getRequestScheme()
104
    {
105 2
        return isset($this->server['REQUEST_SCHEME']) ? $this->server['REQUEST_SCHEME'] : null;
106
    }
107
108
    /**
109
     * Get host
110
     *
111
     * @return string|null
112
     */
113 2
    public function getHost()
114
    {
115 2
        return isset($this->server['HTTP_HOST']) ? $this->server['HTTP_HOST'] : null;
116
    }
117
118
    /**
119
     * Get port
120
     *
121
     * @return int|null
122
     */
123 2
    public function getPort()
124
    {
125 2
        return isset($this->server['SERVER_PORT']) ? $this->server['SERVER_PORT'] : null;
126
    }
127
128
    /**
129
     * Get protocol version
130
     *
131
     * @return string|null
132
     */
133 2
    public function getProtocolVersion()
134
    {
135 2
        $version = null;
136 2
        if (isset($this->server['SERVER_PROTOCOL'])) {
137 1
            $protocol = $this->server['SERVER_PROTOCOL'];
138 1
            list(,$version) = explode("/", $protocol);
139 1
        }
140
141 2
        return $version;
142
    }
143
144
    /**
145
     * Get auth user
146
     *
147
     * @return string|null
148
     */
149 2
    public function getAuthUser()
150
    {
151 2
        if (isset($this->server['PHP_AUTH_USER'])) {
152 1
            return $this->server['PHP_AUTH_USER'];
153
        }
154
155 1
        return null;
156
    }
157
158
    /**
159
     * Get auth password
160
     *
161
     * @return string|null
162
     */
163 2
    public function getAuthPassword()
164
    {
165 2
        if (isset($this->server['PHP_AUTH_PW'])) {
166 1
            return $this->server['PHP_AUTH_PW'];
167
        }
168
169 1
        return null;
170
    }
171
172
    /**
173
     * Get body
174
     *
175
     * @return resource
176
     */
177 1
    public function getBody()
178
    {
179 1
        return $this->body;
180
    }
181
182
    /**
183
     * Get cookies
184
     *
185
     * @return array
186
     */
187 1
    public function getCookies()
188
    {
189 1
        return $this->cookies;
190
    }
191
192
    /**
193
     * Get files
194
     *
195
     * @return array
196
     */
197 1
    public function getFiles()
198
    {
199 1
        return $this->files;
200
    }
201
202
    /**
203
     * Get server
204
     *
205
     * @return array
206
     */
207 1
    public function getServer()
208
    {
209 1
        return $this->server;
210
    }
211
}
212