Sender::toDatabase()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
cc 2
eloc 6
c 6
b 1
f 1
nc 2
nop 3
dl 0
loc 11
rs 9.4285
1
<?php namespace Algorit\Synchronizer;
2
3
use Closure;
4
use Algorit\Synchronizer\Request\RequestInterface;
5
6
class Sender {
7
8
	/**
9
	 * Send data to ERP
10
	 *
11
	 * @return void
12
	 */
13
	public function toErp(RequestInterface $request, $entity, Array $response)
14
	{
15
		if( ! $data = $this->parse($response))
16
		{
17
			return $response;
18
		}
19
20
		return $request->send($entity, $data);
21
	}
22
23
	/**
24
	 * Insert data into Database
25
	 *
26
	 * @return void
27
	 */
28
	public function toDatabase(RequestInterface $request, $entity, Array $response)
29
	{
30
		if( ! $data = $this->parse($response))
31
		{
32
			return $response;
33
		}
34
35
		return $request->getCaller()
36
					   ->repository($entity)
37
					   ->sync($data);
38
	}
39
40
	/**
41
	 * Send data to Device (Api)
42
	 *
43
	 * @return void
44
	 */
45
	public function toApi(Array $data, $parse = false)
46
	{
47
		if($parse instanceof Closure)
48
		{
49
			return $parse($data);
50
		}
51
52
		return $data;
53
	}
54
55
	/**
56
	 * Parse response array
57
	 *
58
	 * @param  array $response
59
	 * @return array
60
	 */	
61
	private function parse(Array $response)
62
	{
63
		$data = array_get($response, 'data');
64
65
		if( ! is_array($data) OR count($data) == 0)
66
		{
67
			return false;
68
		}
69
70
		return $data;
71
	}
72
73
}