Request::executeSendRequest()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 3
b 0
f 0
nc 3
nop 3
dl 0
loc 17
rs 9.2
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation 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.

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<Algorit\Synchronizer\Request\Config> is incompatible with the declared type object<Algorit\Synchronizer\Config> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 .
0 ignored issues
show
Bug introduced by
The variable $content does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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,
0 ignored issues
show
Documentation introduced by
There is no parameter named $requestMethod,. Did you maybe mean $requestMethod?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
310
	 * @param  string  $url
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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,
0 ignored issues
show
Documentation introduced by
There is no parameter named $requestMethod,. Did you maybe mean $requestMethod?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
335
	 * @param  string  $url
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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