Request   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 40
dl 0
loc 215
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getCookies() 0 3 1
A addParam() 0 4 1
A disableSSLValidation() 0 4 1
A addHeader() 0 4 1
A getAuthentication() 0 3 1
A isDisableSSLValidation() 0 3 1
A setAuthentication() 0 4 1
A addCookie() 0 4 1
A getParams() 0 3 1
A getHeaders() 0 3 1
A validate() 0 5 2
1
<?php
2
/**
3
 * @category    Brownie/HttpClient
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\HttpClient;
9
10
use Brownie\HttpClient\Exception\ValidateException;
11
use Brownie\HttpClient\Header\Header;
12
use Brownie\HttpClient\Cookie\Cookie;
13
use Brownie\Util\StorageArray;
14
15
/**
16
 * HTTP client request.
17
 *
18
 * @method Request          setMethod(string $method)           Sets HTTP request method.
19
 * @method string           getMethod()                         Returns HTTP request method.
20
 * @method Request          setUrl(string $url)                 Sets URL request.
21
 * @method null|string      getUrl()                            Returns URL request.
22
 * @method Request          setBody(string $body)               Sets request body.
23
 * @method null|string      getBody()                           Returns request body.
24
 * @method Request          setBodyFormat(string $bodyFormat)   Sets the body data format.
25
 * @method string           getBodyFormat()                     Returns body data format.
26
 * @method Request          setTimeOut(int $timeOut)            Sets the execution time of the request.
27
 * @method string           getTimeOut()                        Returns the maximum time to execute the query.
28
 */
29
class Request extends StorageArray
30
{
31
32
    /**
33
     * Body data formats.
34
     */
35
    const FORMAT_APPLICATION_JSON = 'application/json';
36
37
    const FORMAT_APPLICATION_XML = 'application/xml';
38
39
    const FORMAT_TEXT_HTML = 'text/html';
40
41
    const FORMAT_TEXT_PLAIN = 'text/plain';
42
43
    const FORMAT_FORM_URLENCODED = 'application/x-www-form-urlencoded';
44
45
    /**
46
     * HTTP request method GET.
47
     */
48
    const HTTP_METHOD_GET = 'GET';
49
50
    /**
51
     * HTTP request method POST.
52
     */
53
    const HTTP_METHOD_POST = 'POST';
54
55
    /**
56
     * HTTP request method PUT.
57
     */
58
    const HTTP_METHOD_PUT = 'PUT';
59
60
    /**
61
     * HTTP request method DELETE.
62
     */
63
    const HTTP_METHOD_DELETE = 'DELETE';
64
65
    /**
66
     * HTTP request method HEADER.
67
     */
68
    const HTTP_METHOD_HEADER = 'HEADER';
69
70
    protected $fields = array(
71
        'method' => self::HTTP_METHOD_GET,              // HTTP request method.
72
        'url' => null,                                  // URL request.
73
        'body' => null,                                 // Request body.
74
        'bodyFormat' => self::FORMAT_FORM_URLENCODED,   // Body data format.
75
        'timeOut' => 60,                                // The maximum number of seconds allowed to execute a query.
76
    );
77
78
    /**
79
     * An array of HTTP header fields to set.
80
     *
81
     * @var array
82
     */
83
    private $headers = array();
84
85
    /**
86
     * An array of HTTP cookie fields to set.
87
     *
88
     * @var array
89
     */
90
    private $cookies = array();
91
92
    /**
93
     * An array of GET params to set.
94
     *
95
     * @var array
96
     */
97
    private $params = array();
98
99
    /**
100
     * Flag of checking the server's SSL.
101
     *
102
     * @var bool
103
     */
104
    private $disableSSLValidation = false;
105
106
    /**
107
     * Authentication data.
108
     *
109
     * @var string
110
     */
111
    private $authentication;
112
113
    /**
114
     * Adds GET param.
115
     * Returns the current object.
116
     *
117
     * @param string    $name       GET parameter name.
118
     * @param string    $value      GET parameter value.
119
     *
120
     * @return self
121
     */
122 1
    public function addParam($name, $value)
123
    {
124 1
        $this->params[$name] = $value;
125 1
        return $this;
126
    }
127
128
    /**
129
     * Adds header.
130
     * Returns the current object.
131
     *
132
     * @param Header    $header     Header.
133
     *
134
     * @return self
135
     */
136 1
    public function addHeader(Header $header)
137
    {
138 1
        $this->headers[$header->getName()] = $header;
139 1
        return $this;
140
    }
141
142
    /**
143
     * Adds cookie.
144
     * Returns the current object.
145
     *
146
     * @param Cookie    $cookie     Cookie.
147
     *
148
     * @return self
149
     */
150 1
    public function addCookie(Cookie $cookie)
151
    {
152 1
        $this->cookies[$cookie->getName()] = $cookie;
153 1
        return $this;
154
    }
155
156
    /**
157
     * Returns GET params.
158
     *
159
     * @return array
160
     */
161 1
    public function getParams()
162
    {
163 1
        return $this->params;
164
    }
165
166
    /**
167
     * Returns headers.
168
     *
169
     * @return array
170
     */
171 1
    public function getHeaders()
172
    {
173 1
        return $this->headers;
174
    }
175
176
    /**
177
     * Returns cookies.
178
     *
179
     * @return array
180
     */
181 1
    public function getCookies()
182
    {
183 1
        return $this->cookies;
184
    }
185
186
    /**
187
     * Disables of checking the server's SSL.
188
     * Returns the current object.
189
     *
190
     * @return self
191
     */
192 1
    public function disableSSLValidation()
193
    {
194 1
        $this->disableSSLValidation = true;
195 1
        return $this;
196
    }
197
198
    /**
199
     * Returns flag of checking the server's SSL.
200
     *
201
     * @return bool
202
     */
203 1
    public function isDisableSSLValidation()
204
    {
205 1
        return $this->disableSSLValidation;
206
    }
207
208
    /**
209
     * Validates request data.
210
     *
211
     * @throws ValidateException
212
     */
213 2
    public function validate()
214
    {
215 2
        $url = $this->getUrl();
216 2
        if (empty($url)) {
217 1
            throw new ValidateException('No required fields: url');
218
        }
219 1
    }
220
221
    /**
222
     * Sets authentication data.
223
     * Returns the current object.
224
     *
225
     * @param $login
226
     * @param $password
227
     *
228
     * @return self
229
     */
230 1
    public function setAuthentication($login, $password)
231
    {
232 1
        $this->authentication = $login . ':' . $password;
233 1
        return $this;
234
    }
235
236
    /**
237
     * Returns authentication data.
238
     *
239
     * @return string
240
     */
241 1
    public function getAuthentication()
242
    {
243 1
        return $this->authentication;
244
    }
245
}
246