|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the BuzzDriver class for RestBundle. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient; |
|
10
|
|
|
|
|
11
|
|
|
use Nyholm\Psr7\Request; |
|
12
|
|
|
use Buzz\Client\Curl; |
|
13
|
|
|
|
|
14
|
|
|
class BuzzDriver implements DriverInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use DriverHelper; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Psr\Http\Message\ResponseInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $response = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Host used to prepare Request URI. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $host = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Resource path used to prepare Request URI. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $resource = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* HTTP method used to prepare Request. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $method = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Request headers. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $headers = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Request message body. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
private $body = null; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get response. |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \RuntimeException If request hasn't been send already |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function getResponse() |
|
66
|
|
|
{ |
|
67
|
|
|
if (null !== $this->response) { |
|
68
|
|
|
return $this->response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new \RuntimeException("Attempt to get response data when request hasn't been sent yet"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Prepare and get request. |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Psr\Http\Message\RequestInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function getRequest() |
|
80
|
|
|
{ |
|
81
|
|
|
if (empty($this->method) || empty($this->host)) { |
|
82
|
|
|
throw new \RuntimeException('Attempted to get unspecified Request'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return new Request( |
|
86
|
|
|
$this->method, |
|
87
|
|
|
$this->host . $this->resource, |
|
88
|
|
|
$this->headers, |
|
89
|
|
|
$this->body |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Send the request. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function send() |
|
97
|
|
|
{ |
|
98
|
|
|
// prepare client |
|
99
|
|
|
$curl = new Curl( |
|
100
|
|
|
[ |
|
101
|
|
|
'timeout' => 10, |
|
102
|
|
|
] |
|
103
|
|
|
); |
|
104
|
|
|
$this->response = null; |
|
105
|
|
|
$this->response = $curl->sendRequest($this->getRequest()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Set request host. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $host |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function setHost($host) |
|
114
|
|
|
{ |
|
115
|
|
|
if (substr($host, -1) === '/') { |
|
116
|
|
|
$host = substr($host, 0, strlen($host) - 1); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->host = $host; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set request resource url. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $resource |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setResource($resource) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->resource = $resource; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set request method. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $method Can be GET, POST, PATCH, ... |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setMethod($method) |
|
138
|
|
|
{ |
|
139
|
|
|
if (in_array(strtolower($method), ['publish', 'patch', 'move', 'swap'])) { |
|
140
|
|
|
$this->headers['X-HTTP-Method-Override'] = $method; |
|
141
|
|
|
$this->method = 'POST'; |
|
142
|
|
|
} else { |
|
143
|
|
|
$this->method = strtoupper($method); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get response status code. |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
* |
|
152
|
|
|
* @throws \RuntimeException If request hasn't been send already |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getStatusCode() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->getResponse()->getStatusCode(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get response status message. |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
* |
|
164
|
|
|
* @throws \RuntimeException If request hasn't been send already |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getStatusMessage() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->getResponse()->getReasonPhrase(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set request header. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $header Header to be set |
|
175
|
|
|
* @param mixed $value |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function setHeader($header, $value) |
|
178
|
|
|
{ |
|
179
|
|
|
if (is_array($value)) { |
|
180
|
|
|
$value = implode(';', $value); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$this->headers[$header] = $value; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get all response headers. |
|
188
|
|
|
* |
|
189
|
|
|
* @return array Associative array with $header => $value (value can be an array if it hasn't a single value) |
|
190
|
|
|
* |
|
191
|
|
|
* @throws \RuntimeException If request hasn't been send already |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getHeaders() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->response->getHeaders(); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get response body. |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
* |
|
203
|
|
|
* @throws \RuntimeException If request hasn't been send already |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getBody() |
|
206
|
|
|
{ |
|
207
|
|
|
$bodyStream = $this->getResponse()->getBody(); |
|
208
|
|
|
$contents = $bodyStream->getContents(); |
|
209
|
|
|
$bodyStream->rewind(); |
|
210
|
|
|
|
|
211
|
|
|
return $contents; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Set request body. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $body |
|
218
|
|
|
*/ |
|
219
|
|
|
public function setBody($body) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->body = $body; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|