|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author stev leibelt <[email protected]> |
|
4
|
|
|
* @since 2015-12-08 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Net\Bazzline\Component\Curl\Builder; |
|
7
|
|
|
|
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\HeaderLineInterface; |
|
10
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsJson; |
|
11
|
|
|
use Net\Bazzline\Component\Curl\Option\OptionInterface; |
|
12
|
|
|
use Net\Bazzline\Component\Curl\Request\Request; |
|
13
|
|
|
use Net\Bazzline\Component\Curl\Response\Response; |
|
14
|
|
|
use Net\Bazzline\Component\Curl\ResponseBehaviour\ConvertJsonToArrayBehaviour; |
|
15
|
|
|
use Net\Bazzline\Component\Curl\ResponseBehaviour\ResponseBehaviourInterface; |
|
16
|
|
|
use Net\Bazzline\Component\Toolbox\HashMap\Merge; |
|
17
|
|
|
use RuntimeException; |
|
18
|
|
|
|
|
19
|
|
|
class Builder |
|
20
|
|
|
{ |
|
21
|
|
|
const METHOD_DELETE = 0; |
|
22
|
|
|
const METHOD_GET = 1; |
|
23
|
|
|
const METHOD_PATCH = 2; |
|
24
|
|
|
const METHOD_POST = 3; |
|
25
|
|
|
const METHOD_PUT = 4; |
|
26
|
|
|
|
|
27
|
|
|
/** @var boolean */ |
|
28
|
|
|
private $asJson; |
|
29
|
|
|
|
|
30
|
|
|
/** @var null|string|array */ |
|
31
|
|
|
private $data; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array|ResponseBehaviourInterface[] */ |
|
34
|
|
|
private $defaultResponseBehaviours; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Merge */ |
|
37
|
|
|
private $merge; |
|
38
|
|
|
|
|
39
|
|
|
/** @var int */ |
|
40
|
|
|
private $method; |
|
41
|
|
|
|
|
42
|
|
|
/** @var array */ |
|
43
|
|
|
private $parameters; |
|
44
|
|
|
|
|
45
|
|
|
/** @var Request */ |
|
46
|
|
|
private $request; |
|
47
|
|
|
|
|
48
|
|
|
/** @var array|ResponseBehaviourInterface[] */ |
|
49
|
|
|
private $responseBehaviours; |
|
50
|
|
|
|
|
51
|
|
|
/** @var string */ |
|
52
|
|
|
private $url; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param Request $request |
|
56
|
|
|
* @param Merge $merge |
|
57
|
|
|
* @param array|ResponseBehaviourInterface[] $defaultResponseBehaviours |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(Request $request, Merge $merge, array $defaultResponseBehaviours = array()) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->defaultResponseBehaviours = $defaultResponseBehaviours; |
|
62
|
|
|
$this->merge = $merge; |
|
63
|
|
|
$this->request = $request; |
|
64
|
|
|
$this->reset(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Response |
|
69
|
|
|
* @throws Exception|RuntimeException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function andFetchTheResponse() |
|
72
|
|
|
{ |
|
73
|
|
|
//begin of dependencies |
|
74
|
|
|
$asJson = $this->asJson; |
|
75
|
|
|
$merge = $this->merge; |
|
76
|
|
|
$data = $this->data; |
|
77
|
|
|
$method = $this->method; |
|
78
|
|
|
$parameters = $this->parameters; |
|
79
|
|
|
$request = $this->request; |
|
80
|
|
|
$url = $this->url; |
|
81
|
|
|
//end of dependencies |
|
82
|
|
|
|
|
83
|
|
|
//begin of business logic |
|
84
|
|
|
/** @var ResponseBehaviourInterface[] $behaviours */ |
|
85
|
|
|
$behaviours = $merge($this->responseBehaviours, $this->defaultResponseBehaviours); |
|
86
|
|
|
$data = $this->convertToJsonIfNeeded($data, $asJson); |
|
87
|
|
|
$response = $this->fetchResponseFromRequestOrThrowRuntimeException( |
|
88
|
|
|
$method, |
|
89
|
|
|
$request, |
|
90
|
|
|
$url, |
|
91
|
|
|
$parameters, |
|
92
|
|
|
$data |
|
93
|
|
|
); |
|
94
|
|
|
$response = $this->applyBehaviours($behaviours, $response); |
|
95
|
|
|
//end of business logic |
|
96
|
|
|
|
|
97
|
|
|
return $response; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function asJson() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->asJson = true; |
|
106
|
|
|
$this->request->addHeaderLine(new ContentTypeIsJson()); |
|
107
|
|
|
$this->responseBehaviours[] = new ConvertJsonToArrayBehaviour(); |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param bool $alsoTheDefaults |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function reset($alsoTheDefaults = false) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->asJson = false; |
|
119
|
|
|
$this->data = null; |
|
120
|
|
|
$this->parameters = array(); |
|
121
|
|
|
$this->request->reset($alsoTheDefaults); |
|
122
|
|
|
$this->responseBehaviours = array(); |
|
123
|
|
|
$this->url = null; |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $url |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
|
|
public function onTheUrl($url) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->url = $url; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param null|string|array $data |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
|
|
public function withTheData($data) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->data = $data; |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param HeaderLineInterface $line |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function withTheHeaderLine(HeaderLineInterface $line) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->request->addHeaderLine($line); |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param OptionInterface $option |
|
163
|
|
|
* @return $this |
|
164
|
|
|
*/ |
|
165
|
|
|
public function withTheOption(OptionInterface $option) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->request->addOption($option); |
|
168
|
|
|
|
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $parameters |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
|
|
public function withTheParameters(array $parameters) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->parameters = $parameters; |
|
179
|
|
|
|
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param string $line |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
|
|
public function withTheRawHeaderLine($line) |
|
188
|
|
|
{ |
|
189
|
|
|
$this->request->addRawHeaderLine($line); |
|
190
|
|
|
|
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $key |
|
196
|
|
|
* @param string $value |
|
197
|
|
|
* @return $this |
|
198
|
|
|
*/ |
|
199
|
|
|
public function withTheRawOption($key, $value) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->request->addRawOption($key, $value); |
|
202
|
|
|
|
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param ResponseBehaviourInterface $behaviour |
|
208
|
|
|
* @return $this |
|
209
|
|
|
*/ |
|
210
|
|
|
public function withTheResponseBehaviour(ResponseBehaviourInterface $behaviour) |
|
211
|
|
|
{ |
|
212
|
|
|
$this->responseBehaviours[] = $behaviour; |
|
213
|
|
|
|
|
214
|
|
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
//@todo better nameing? |
|
218
|
|
|
// callDelete |
|
219
|
|
|
/** |
|
220
|
|
|
* @return $this |
|
221
|
|
|
*/ |
|
222
|
|
|
public function useDelete() |
|
223
|
|
|
{ |
|
224
|
|
|
$this->method = self::METHOD_DELETE; |
|
225
|
|
|
|
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return $this |
|
231
|
|
|
*/ |
|
232
|
|
|
public function useGet() |
|
233
|
|
|
{ |
|
234
|
|
|
$this->method = self::METHOD_GET; |
|
235
|
|
|
|
|
236
|
|
|
return $this; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return $this |
|
241
|
|
|
*/ |
|
242
|
|
|
public function usePatch() |
|
243
|
|
|
{ |
|
244
|
|
|
$this->method = self::METHOD_PATCH; |
|
245
|
|
|
|
|
246
|
|
|
return $this; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return $this |
|
251
|
|
|
*/ |
|
252
|
|
|
public function usePost() |
|
253
|
|
|
{ |
|
254
|
|
|
$this->method = self::METHOD_POST; |
|
255
|
|
|
|
|
256
|
|
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return $this |
|
261
|
|
|
*/ |
|
262
|
|
|
public function usePut() |
|
263
|
|
|
{ |
|
264
|
|
|
$this->method = self::METHOD_PUT; |
|
265
|
|
|
|
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param array|ResponseBehaviourInterface[] $behaviours |
|
271
|
|
|
* @param Response $response |
|
272
|
|
|
* @return Response |
|
273
|
|
|
*/ |
|
274
|
|
|
private function applyBehaviours(array $behaviours, Response $response) |
|
275
|
|
|
{ |
|
276
|
|
|
foreach ($behaviours as $behaviour) { |
|
277
|
|
|
$response = $behaviour->behave($response); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
return $response; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @param mixed $data |
|
285
|
|
|
* @param boolean $convertIt |
|
286
|
|
|
* @return mixed |
|
287
|
|
|
*/ |
|
288
|
|
|
private function convertToJsonIfNeeded($data, $convertIt) |
|
289
|
|
|
{ |
|
290
|
|
|
return ($convertIt) ? json_encode($data) : $data; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @param string $method |
|
295
|
|
|
* @param Request $request |
|
296
|
|
|
* @param string $url |
|
297
|
|
|
* @param array $parameters |
|
298
|
|
|
* @param mixed $data |
|
299
|
|
|
* @return Response |
|
300
|
|
|
* @throws RuntimeException |
|
301
|
|
|
*/ |
|
302
|
|
|
private function fetchResponseFromRequestOrThrowRuntimeException($method, Request $request, $url, array $parameters, $data) |
|
303
|
|
|
{ |
|
304
|
|
|
switch ($method) { |
|
305
|
|
|
case self::METHOD_DELETE: |
|
306
|
|
|
$response = $request->delete($url, $parameters); |
|
307
|
|
|
break; |
|
308
|
|
|
case self::METHOD_GET: |
|
309
|
|
|
$response = $request->get($url, $parameters); |
|
310
|
|
|
break; |
|
311
|
|
|
case self::METHOD_PATCH: |
|
312
|
|
|
$response = $request->patch($url, $parameters, $data); |
|
313
|
|
|
break; |
|
314
|
|
|
case self::METHOD_POST: |
|
315
|
|
|
$response = $request->post($url, $parameters, $data); |
|
316
|
|
|
break; |
|
317
|
|
|
case self::METHOD_PUT: |
|
318
|
|
|
$response = $request->put($url, $parameters, $data); |
|
319
|
|
|
break; |
|
320
|
|
|
default: |
|
321
|
|
|
throw new RuntimeException( |
|
322
|
|
|
'no http method set' |
|
323
|
|
|
); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return $response; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|