|
1
|
|
|
<?php namespace Algorit\Synchronizer\Request; |
|
2
|
|
|
|
|
3
|
|
|
use Closure; |
|
4
|
|
|
use Carbon\Carbon; |
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
6
|
|
|
use Algorit\Synchronizer\Request\Methods\MethodInterface; |
|
7
|
|
|
use Algorit\Synchronizer\Request\Contracts\ResourceInterface; |
|
8
|
|
|
use Algorit\Synchronizer\Request\Contracts\DispatcherInterface; |
|
9
|
|
|
use Algorit\Synchronizer\Request\Exceptions\RequestException; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Request implements RequestInterface { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The method instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \Algorit\Synchronizer\Request\Methods\MethodInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $method; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The Caller instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \Algorit\Synchronizer\Request\Caller |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $caller; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The Config instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Algorit\Synchronizer\Config |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $config; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The Resource instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Algorit\Synchronizer\Request\Contracts\ResourceInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $resource; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The Dispatcher instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @var \Algorit\Synchronizer\Request\Contracts\DispatcherInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $dispatcher; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The http cookie. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $cookie; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The http headers. |
|
57
|
|
|
* |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $headers; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The request options. |
|
64
|
|
|
* |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $options; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create a new instance. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Algorit\Synchronizer\Request\Methods\MethodInterface $method |
|
73
|
|
|
* @param \Algorit\Synchronizer\Request\Caller $caller |
|
74
|
|
|
* @return instance |
|
|
|
|
|
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(MethodInterface $method, Caller $caller) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->method = $method; |
|
79
|
|
|
$this->caller = $caller; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set the Config instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @param \Algorit\Synchronizer\Request\Config |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setConfig(Config $config) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->config = $config; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set the Resource instance. |
|
95
|
|
|
* |
|
96
|
|
|
* @param \Algorit\Synchronizer\Request\Contracts\ResourceInterface |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setResource(ResourceInterface $resource) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->resource = $resource; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the Dispatcher instance. |
|
106
|
|
|
* |
|
107
|
|
|
* @param \Algorit\Synchronizer\Request\Contracts\DispatcherInterface |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setDispatcher(DispatcherInterface $dispatcher) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->dispatcher = $dispatcher; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set the request options. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $entityName |
|
119
|
|
|
* @param string $lastSync |
|
120
|
|
|
* @param string $type |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setOptions($entityName, $lastSync = false, $type = 'receive') |
|
124
|
|
|
{ |
|
125
|
|
|
$entities = $this->config->getEntities(); |
|
126
|
|
|
|
|
127
|
|
|
if( ! in_array($type, array('receive', 'send'))) |
|
128
|
|
|
{ |
|
129
|
|
|
throw new RequestException('Wrong request type'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if( ! isset($entities[$type][$entityName])) |
|
133
|
|
|
{ |
|
134
|
|
|
throw new RequestException('Entity not found in system config file.'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if( ! $lastSync instanceof Carbon) |
|
138
|
|
|
{ |
|
139
|
|
|
$lastSync = Carbon::createFromFormat($this->config->date['format'], $this->config->date['default']); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->options = (object) [ |
|
143
|
|
|
'base_url' => array_get($this->config->config, 'base_url'), |
|
144
|
|
|
'type' => $type, |
|
145
|
|
|
'lastSync' => $lastSync, |
|
146
|
|
|
'entity' => $entities[$type][$entityName] |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get the Config instance. |
|
152
|
|
|
* |
|
153
|
|
|
* @return \Algorit\Synchronizer\Request\Config |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getConfig() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->config; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get the Resource instance. |
|
162
|
|
|
* |
|
163
|
|
|
* @return \Algorit\Synchronizer\Request\Contracts\ResourceInterface |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getResource() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->resource; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Get the Caller instance. |
|
172
|
|
|
* |
|
173
|
|
|
* @return \Algorit\Synchronizer\Request\Caller |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getCaller() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->caller; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Get the request options |
|
182
|
|
|
* |
|
183
|
|
|
* @param void |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getOptions() |
|
187
|
|
|
{ |
|
188
|
|
|
$this->options->url = $this->getRequestUrl(); |
|
189
|
|
|
|
|
190
|
|
|
return $this->options; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get the request URL with the last sync date |
|
195
|
|
|
* |
|
196
|
|
|
* @param void, |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
private function getRequestUrl() |
|
200
|
|
|
{ |
|
201
|
|
|
// Set URL |
|
202
|
|
|
$url = array_get($this->config->config, 'base_url') . '/' . array_get($this->options->entity, 'url'); |
|
203
|
|
|
|
|
204
|
|
|
// Get lastSync date |
|
205
|
|
|
$lastSync = $this->options->lastSync->format($this->config->date['format']); |
|
206
|
|
|
|
|
207
|
|
|
// Todo: Use Sender or Receiver |
|
208
|
|
|
if($this->options->type == 'receive') |
|
209
|
|
|
{ |
|
210
|
|
|
// Add date to URL on Receive requests. |
|
211
|
|
|
$url .= '?' . $this->config->date['query_string'] . '=' . str_replace(' ', '_', $lastSync); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return $url; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Create a request to authenticate. |
|
219
|
|
|
* |
|
220
|
|
|
* Needs to be implemented by subclasses. |
|
221
|
|
|
* |
|
222
|
|
|
* @return mixed |
|
223
|
|
|
*/ |
|
224
|
|
|
public abstract function authenticate(); |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Process the data received from a request. |
|
228
|
|
|
* |
|
229
|
|
|
* @param mixed $request |
|
230
|
|
|
* @param \Closure $callback |
|
231
|
|
|
* @return mixed |
|
232
|
|
|
*/ |
|
233
|
|
|
public function processRequestData($request, Closure $callback) |
|
234
|
|
|
{ |
|
235
|
|
|
return $callback(json_decode($request->body, true)); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Execute a request. |
|
240
|
|
|
* |
|
241
|
|
|
* Needs to be implemented by subclasses. |
|
242
|
|
|
* |
|
243
|
|
|
* @param boolean $auth |
|
244
|
|
|
* @return \Algorit\Synchronizer\Request\Methods\MethodInterface |
|
245
|
|
|
*/ |
|
246
|
|
|
public abstract function executeRequest($auth = true); |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Create a request to receive data. |
|
250
|
|
|
* |
|
251
|
|
|
* Needs to be implemented by subclasses. |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $entityName |
|
254
|
|
|
* @param mixed $lastSync |
|
255
|
|
|
* @return \Algorit\Synchronizer\Request\Methods\MethodInterface |
|
256
|
|
|
*/ |
|
257
|
|
|
public abstract function receive($entityName, $lastSync); |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Create a request to send data. |
|
261
|
|
|
* |
|
262
|
|
|
* Needs to be implemented by subclasses. |
|
263
|
|
|
* |
|
264
|
|
|
* @param array $data |
|
265
|
|
|
* @param string $entityName |
|
266
|
|
|
* @param mixed $lastSync |
|
267
|
|
|
* @return \Algorit\Synchronizer\Request\Methods\MethodInterface |
|
268
|
|
|
*/ |
|
269
|
|
|
public abstract function send($entityName, Array $data, $lastSync = false); |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Create a multipart request. (For file uploads) |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $inputName |
|
275
|
|
|
* @param string $file |
|
276
|
|
|
* @param string $fileName |
|
277
|
|
|
* @return array |
|
278
|
|
|
*/ |
|
279
|
|
|
protected function createMultipartRequest($inputName, $file, $fileName) |
|
|
|
|
|
|
280
|
|
|
{ |
|
281
|
|
|
$separator = '----' . md5($fileName); |
|
282
|
|
|
$eol = "\r\n"; |
|
283
|
|
|
|
|
284
|
|
|
// Use filesystem? Call parsers? |
|
285
|
|
|
// $content = $this->files->get($file); |
|
286
|
|
|
|
|
287
|
|
|
$headers = array( |
|
288
|
|
|
'Cookie' => $this->headers['Cookie'], |
|
289
|
|
|
'Content-Type' => 'multipart/form-data; boundary=' . $separator, |
|
290
|
|
|
'Connection' => 'keep-alive' |
|
291
|
|
|
); |
|
292
|
|
|
|
|
293
|
|
|
$body = '--' . $separator . $eol . |
|
294
|
|
|
'Content-Disposition: form-data; name="' . $inputName . '"; filename="' . $fileName . '.zip"' . $eol . |
|
295
|
|
|
'Content-Type: application/octet-stream' . $eol . |
|
296
|
|
|
$eol . $content . |
|
|
|
|
|
|
297
|
|
|
$eol . $eol . |
|
298
|
|
|
'--' . $separator; |
|
299
|
|
|
|
|
300
|
|
|
return array( |
|
301
|
|
|
'headers' => $headers, |
|
302
|
|
|
'body' => $body |
|
303
|
|
|
); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Execute a request to Send data. |
|
308
|
|
|
* |
|
309
|
|
|
* @param string $requestMethod, |
|
|
|
|
|
|
310
|
|
|
* @param string $url |
|
|
|
|
|
|
311
|
|
|
* @return \Algorit\Synchronizer\Request\Methods\MethodInterface |
|
312
|
|
|
*/ |
|
313
|
|
|
protected function executeSendRequest($requestMethod, $data, $options = array()) |
|
314
|
|
|
{ |
|
315
|
|
|
if( ! isset($data['headers']) or ! isset($data['body'])) |
|
316
|
|
|
{ |
|
317
|
|
|
throw new RequestException('Wrong send request data format'); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
$headers = array_get($data, 'headers'); |
|
321
|
|
|
$body = array_get($data, 'body'); |
|
322
|
|
|
|
|
323
|
|
|
if( ! isset($options['timeout'])) |
|
324
|
|
|
{ |
|
325
|
|
|
$options = array_merge($options, array('timeout' => 200000)); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
return $this->method->{$requestMethod}($this->getRequestUrl(), $headers, $body, $options); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Execute a request to Receive data. |
|
333
|
|
|
* |
|
334
|
|
|
* @param string $requestMethod, |
|
|
|
|
|
|
335
|
|
|
* @param string $url |
|
|
|
|
|
|
336
|
|
|
* @return \Algorit\Synchronizer\Request\Methods\MethodInterface |
|
337
|
|
|
*/ |
|
338
|
|
|
protected function executeReceiveRequest($requestMethod, $options = array()) |
|
339
|
|
|
{ |
|
340
|
|
|
if( ! isset($options['timeout'])) |
|
341
|
|
|
{ |
|
342
|
|
|
$options = array_merge($options, array('timeout' => 200000)); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
return $this->method->{$requestMethod}($this->getRequestUrl(), $this->headers, $options); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
} |
|
349
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.