Completed
Pull Request — master (#271)
by
unknown
03:48
created

Session::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 4
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Rmccue\Requests;
3
4
use Rmccue\Requests as Requests;
5
6
use Rmccue\Requests\IRI as IRI;
7
use Rmccue\Requests\Cookie\Jar as Cookie_Jar;
8
/**
9
 * Session handler for persistent requests and default parameters
10
 *
11
 * @package Rmccue\Requests
12
 * @subpackage Session Handler
13
 */
14
15
/**
16
 * Session handler for persistent requests and default parameters
17
 *
18
 * Allows various options to be set as default values, and merges both the
19
 * options and URL properties together. A base URL can be set for all requests,
20
 * with all subrequests resolved from this. Base options can be set (including
21
 * a shared cookie jar), then overridden for individual requests.
22
 *
23
 * @package Rmccue\Requests
24
 * @subpackage Session Handler
25
 */
26
class Session {
27
	/**
28
	 * Base URL for requests
29
	 *
30
	 * URLs will be made absolute using this as the base
31
	 * @var string|null
32
	 */
33
	public $url = null;
34
35
	/**
36
	 * Base headers for requests
37
	 * @var array
38
	 */
39
	public $headers = array();
40
41
	/**
42
	 * Base data for requests
43
	 *
44
	 * If both the base data and the per-request data are arrays, the data will
45
	 * be merged before sending the request.
46
	 *
47
	 * @var array
48
	 */
49
	public $data = array();
50
51
	/**
52
	 * Base options for requests
53
	 *
54
	 * The base options are merged with the per-request data for each request.
55
	 * The only default option is a shared cookie jar between requests.
56
	 *
57
	 * Values here can also be set directly via properties on the Session
58
	 * object, e.g. `$session->useragent = 'X';`
59
	 *
60
	 * @var array
61
	 */
62
	public $options = array();
63
64
	/**
65
	 * Create a new session
66
	 *
67
	 * @param string|null $url Base URL for requests
68
	 * @param array $headers Default headers for requests
69
	 * @param array $data Default data for requests
70
	 * @param array $options Default options for requests
71
	 */
72
	public function __construct($url = null, $headers = array(), $data = array(), $options = array()) {
73
		$this->url = $url;
74
		$this->headers = $headers;
75
		$this->data = $data;
76
		$this->options = $options;
77
78
		if (empty($this->options['cookies'])) {
79
			$this->options['cookies'] = new Cookie_Jar();
80
		}
81
	}
82
83
	/**
84
	 * Get a property's value
85
	 *
86
	 * @param string $key Property key
87
	 * @return mixed|null Property value, null if none found
88
	 */
89
	public function __get($key) {
90
		if (isset($this->options[$key])) {
91
			return $this->options[$key];
92
		}
93
94
		return null;
95
	}
96
97
	/**
98
	 * Set a property's value
99
	 *
100
	 * @param string $key Property key
101
	 * @param mixed $value Property value
102
	 */
103
	public function __set($key, $value) {
104
		$this->options[$key] = $value;
105
	}
106
107
	/**
108
	 * Remove a property's value
109
	 *
110
	 * @param string $key Property key
111
	 */
112
	public function __isset($key) {
113
		return isset($this->options[$key]);
114
	}
115
116
	/**
117
	 * Remove a property's value
118
	 *
119
	 * @param string $key Property key
120
	 */
121
	public function __unset($key) {
122
		if (isset($this->options[$key])) {
123
			unset($this->options[$key]);
124
		}
125
	}
126
127
	/**#@+
128
	 * @see request()
129
	 * @param string $url
130
	 * @param array $headers
131
	 * @param array $options
132
	 * @return Rmccue\Requests\Response
133
	 */
134
	/**
135
	 * Send a GET request
136
	 */
137
	public function get($url, $headers = array(), $options = array()) {
138
		return $this->request($url, $headers, null, Requests::GET, $options);
139
	}
140
141
	/**
142
	 * Send a HEAD request
143
	 */
144
	public function head($url, $headers = array(), $options = array()) {
145
		return $this->request($url, $headers, null, Requests::HEAD, $options);
146
	}
147
148
	/**
149
	 * Send a DELETE request
150
	 */
151
	public function delete($url, $headers = array(), $options = array()) {
152
		return $this->request($url, $headers, null, Requests::DELETE, $options);
153
	}
154
	/**#@-*/
155
156
	/**#@+
157
	 * @see request()
158
	 * @param string $url
159
	 * @param array $headers
160
	 * @param array $data
161
	 * @param array $options
162
	 * @return Rmccue\Requests\Response
163
	 */
164
	/**
165
	 * Send a POST request
166
	 */
167
	public function post($url, $headers = array(), $data = array(), $options = array()) {
168
		return $this->request($url, $headers, $data, Requests::POST, $options);
169
	}
170
171
	/**
172
	 * Send a PUT request
173
	 */
174
	public function put($url, $headers = array(), $data = array(), $options = array()) {
175
		return $this->request($url, $headers, $data, Requests::PUT, $options);
176
	}
177
178
	/**
179
	 * Send a PATCH request
180
	 *
181
	 * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
182
	 * specification recommends that should send an ETag
183
	 *
184
	 * @link https://tools.ietf.org/html/rfc5789
185
	 */
186
	public function patch($url, $headers, $data = array(), $options = array()) {
187
		return $this->request($url, $headers, $data, Requests::PATCH, $options);
188
	}
189
	/**#@-*/
190
191
	/**
192
	 * Main interface for HTTP requests
193
	 *
194
	 * This method initiates a request and sends it via a transport before
195
	 * parsing.
196
	 *
197
	 * @see Requests::request()
198
	 *
199
	 * @throws Requests_Exception On invalid URLs (`nonhttp`)
200
	 *
201
	 * @param string $url URL to request
202
	 * @param array $headers Extra headers to send with the request
203
	 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
204
	 * @param string $type HTTP request type (use Requests constants)
205
	 * @param array $options Options for the request (see {@see Requests::request})
206
	 * @return Requests_Response
207
	 */
208
	public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) {
209
		$request = $this->merge_request(compact('url', 'headers', 'data', 'options'));
210
211
		return Requests::request($request['url'], $request['headers'], $request['data'], $type, $request['options']);
212
	}
213
214
	/**
215
	 * Send multiple HTTP requests simultaneously
216
	 *
217
	 * @see Requests::request_multiple()
218
	 *
219
	 * @param array $requests Requests data (see {@see Requests::request_multiple})
220
	 * @param array $options Global and default options (see {@see Requests::request})
221
	 * @return array Responses (either Requests_Response or a Requests_Exception object)
222
	 */
223
	public function request_multiple($requests, $options = array()) {
224
		foreach ($requests as $key => $request) {
225
			$requests[$key] = $this->merge_request($request, false);
226
		}
227
228
		$options = array_merge($this->options, $options);
229
230
		// Disallow forcing the type, as that's a per request setting
231
		unset($options['type']);
232
233
		return Requests::request_multiple($requests, $options);
234
	}
235
236
	/**
237
	 * Merge a request's data with the default data
238
	 *
239
	 * @param array $request Request data (same form as {@see request_multiple})
240
	 * @param boolean $merge_options Should we merge options as well?
241
	 * @return array Request data
242
	 */
243
	protected function merge_request($request, $merge_options = true) {
244
		if ($this->url !== null) {
245
			$request['url'] = IRI::absolutize($this->url, $request['url']);
246
			$request['url'] = $request['url']->uri;
247
		}
248
249
		if (empty($request['headers'])) {
250
			$request['headers'] = array();
251
		}
252
		$request['headers'] = array_merge($this->headers, $request['headers']);
253
254
		if (empty($request['data'])) {
255
			if (is_array($this->data)) {
256
				$request['data'] = $this->data;
257
			}
258
		}
259
		elseif (is_array($request['data']) && is_array($this->data)) {
260
			$request['data'] = array_merge($this->data, $request['data']);
261
		}
262
263
		if ($merge_options !== false) {
264
			$request['options'] = array_merge($this->options, $request['options']);
265
266
			// Disallow forcing the type, as that's a per request setting
267
			unset($request['options']['type']);
268
		}
269
270
		return $request;
271
	}
272
}
273