1
|
|
|
<?php namespace Algorit\Synchronizer; |
2
|
|
|
|
3
|
|
|
use Closure; |
4
|
|
|
use Exception; |
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Container\Container; |
8
|
|
|
use Algorit\Synchronizer\Request\Config; |
9
|
|
|
use Algorit\Synchronizer\Request\Contracts\SystemInterface; |
10
|
|
|
use Algorit\Synchronizer\Request\Contracts\ResourceInterface; |
11
|
|
|
use Algorit\Synchronizer\Request\Contracts\ContainerInterface; |
12
|
|
|
|
13
|
|
|
class Loader { |
14
|
|
|
|
15
|
|
|
use LoggerTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The Company instance. |
19
|
|
|
* |
20
|
|
|
* @var object |
21
|
|
|
*/ |
22
|
|
|
protected $company; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The Request instance. |
26
|
|
|
* |
27
|
|
|
* @var object |
28
|
|
|
*/ |
29
|
|
|
protected $request; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The System instance. |
33
|
|
|
* |
34
|
|
|
* @var object |
35
|
|
|
*/ |
36
|
|
|
protected $system; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Builder instance. |
40
|
|
|
* |
41
|
|
|
* @var \Algorit\Synchronizer\Builder |
42
|
|
|
*/ |
43
|
|
|
protected $builder; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The Container instance. |
47
|
|
|
* |
48
|
|
|
* @var \Algorit\Synchronizer\Container |
49
|
|
|
*/ |
50
|
|
|
protected $container; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The Config instance. |
54
|
|
|
* |
55
|
|
|
* @var \Algorit\Synchronizer\Config |
56
|
|
|
*/ |
57
|
|
|
public $config; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new Loader. |
61
|
|
|
* |
62
|
|
|
* @param Builder $builder |
63
|
|
|
* @param Config $config |
64
|
|
|
* @return instance |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public function __construct(Container $container, Builder $builder, Config $config) |
67
|
|
|
{ |
68
|
|
|
$this->config = $config; |
|
|
|
|
69
|
|
|
$this->builder = $builder; |
70
|
|
|
$this->container = $container; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the System instance |
75
|
|
|
* |
76
|
|
|
* @param \Algorit\Synchronizer\Request\Contracts\SystemInterface $system |
77
|
|
|
* @return \Algorit\Synchronizer\Loader |
78
|
|
|
*/ |
79
|
|
|
public function setSystem(SystemInterface $system) |
80
|
|
|
{ |
81
|
|
|
$this->system = $system; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the System instance |
88
|
|
|
* |
89
|
|
|
* @param void |
90
|
|
|
* @return \Algorit\Synchronizer\System |
91
|
|
|
*/ |
92
|
|
|
public function getSystem() |
93
|
|
|
{ |
94
|
|
|
return $this->system; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the Builder instance |
99
|
|
|
* |
100
|
|
|
* @param void |
101
|
|
|
* @return \Algorit\Synchronizer\Builder |
102
|
|
|
*/ |
103
|
|
|
public function getBuilder() |
104
|
|
|
{ |
105
|
|
|
return $this->builder; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the System instance |
110
|
|
|
* |
111
|
|
|
* @param void |
112
|
|
|
* @return \Algorit\Synchronizer\Request\Request |
113
|
|
|
*/ |
114
|
|
|
public function getRequest() |
115
|
|
|
{ |
116
|
|
|
return $this->request; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Load the ERP Request. |
121
|
|
|
* |
122
|
|
|
* @param \Algorit\Synchronizer\Request\Contracts\SystemInterface $system |
123
|
|
|
* @param mixed $callback |
124
|
|
|
* @return \Algorit\Synchronizer\Loader |
125
|
|
|
*/ |
126
|
|
|
public function loadSystem(SystemInterface $system, $callback = false) |
127
|
|
|
{ |
128
|
|
|
$this->logger->notice('Loading ' . $system->name); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$this->setSystem($system); |
131
|
|
|
|
132
|
|
|
// Load system |
133
|
|
|
$this->request = $this->system->loadRequest($this->container); |
134
|
|
|
|
135
|
|
|
return $this->select($system->getResource(), $callback); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Test if the variable is a collection or a resource. |
140
|
|
|
* |
141
|
|
|
* @param mixed $resource |
142
|
|
|
* @param mixed $callback |
143
|
|
|
* @return \Algorit\Synchronizer\Loader |
144
|
|
|
*/ |
145
|
|
|
private function select($resource, $callback) |
146
|
|
|
{ |
147
|
|
|
if($resource == false) |
148
|
|
|
{ |
149
|
|
|
throw new Exception('Resource is not defined.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if($resource instanceof Collection) |
153
|
|
|
{ |
154
|
|
|
return $this->startCollection($resource, $callback); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if($resource instanceof ResourceInterface) |
158
|
|
|
{ |
159
|
|
|
return $this->start($resource, $callback); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Start the loader given a Collection as resource. |
167
|
|
|
* |
168
|
|
|
* @param \Illuminate\Support\Collection $collection |
169
|
|
|
* @param mixed $callback |
170
|
|
|
* @return \Algorit\Synchronizer\Loader |
171
|
|
|
*/ |
172
|
|
|
public function startCollection(Collection $collection, $callback = false) |
173
|
|
|
{ |
174
|
|
|
foreach($collection as $resource) |
175
|
|
|
{ |
176
|
|
|
$this->start($resource, $callback); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set the resource and start the builder. |
184
|
|
|
* |
185
|
|
|
* @param \Algorit\Synchronizer\Request\Contracts\ResourceInterface $resource |
186
|
|
|
* @param mixed $callback |
187
|
|
|
* @return \Algorit\Synchronizer\Loader |
188
|
|
|
*/ |
189
|
|
|
public function start(ResourceInterface $resource, $callback = false) |
190
|
|
|
{ |
191
|
|
|
// Set config |
192
|
|
|
$this->request->setConfig($this->config->setup($this->system, $resource)); |
193
|
|
|
|
194
|
|
|
// Set system resource |
195
|
|
|
$this->request->setResource($resource); |
196
|
|
|
|
197
|
|
|
// Set logger |
198
|
|
|
$this->builder->setLogger($this->logger); |
199
|
|
|
|
200
|
|
|
// Start the Builder |
201
|
|
|
$this->builder->start($this->request, $resource); |
202
|
|
|
|
203
|
|
|
if($callback instanceof Closure) |
204
|
|
|
{ |
205
|
|
|
return $callback($this); // return? |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
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.