1 | <?php namespace Algorit\Synchronizer; |
||
11 | class Builder { |
||
12 | |||
13 | use LoggerTrait; |
||
14 | |||
15 | /** |
||
16 | * The system request instance. |
||
17 | * |
||
18 | * @var object |
||
19 | */ |
||
20 | public $request; |
||
21 | |||
22 | /** |
||
23 | * The current resource |
||
24 | * |
||
25 | * @var object |
||
26 | */ |
||
27 | protected $resource; |
||
28 | |||
29 | /** |
||
30 | * The database repository instance. |
||
31 | * |
||
32 | * @var object |
||
33 | */ |
||
34 | protected $repository; |
||
35 | |||
36 | /** |
||
37 | * The Sender instance. |
||
38 | * |
||
39 | * @var Sender |
||
40 | */ |
||
41 | protected $send; |
||
42 | |||
43 | /** |
||
44 | * The Receiver instance. |
||
45 | * |
||
46 | * @var Receiver |
||
47 | */ |
||
48 | protected $receive; |
||
49 | |||
50 | /** |
||
51 | * Class constructor. |
||
52 | * |
||
53 | * @param \Algorit\Synchronizer\Sender $send |
||
54 | * @param \Algorit\Synchronizer\Receiver $receive |
||
55 | * @param \Algorit\Synchronizer\Storage\SyncRepositoryInterface $repository |
||
56 | * @return void |
||
|
|||
57 | */ |
||
58 | public function __construct(Sender $send, Receiver $receive, SyncRepositoryInterface $repository) |
||
64 | |||
65 | /** |
||
66 | * Set the Request and Resource |
||
67 | * |
||
68 | * @param \Algorit\Synchronizer\Request\Contracts\RequestInterface $request |
||
69 | * @param \Algorit\Synchronizer\Request\Contracts\ResourceInterface $resource |
||
70 | * @return void |
||
71 | */ |
||
72 | public function start(RequestInterface $request, ResourceInterface $resource) |
||
77 | |||
78 | /** |
||
79 | * Get the request |
||
80 | * |
||
81 | * @param void |
||
82 | * @return \Algorit\Synchronizer\Request\Contracts\RequestInterface |
||
83 | */ |
||
84 | public function getRequest() |
||
88 | |||
89 | /** |
||
90 | * Get the resource |
||
91 | * |
||
92 | * @param void |
||
93 | * @return \Algorit\Synchronizer\Request\Contracts\ResourceInterface |
||
94 | */ |
||
95 | public function getResource() |
||
99 | |||
100 | /** |
||
101 | * Create a current sync repository instance. |
||
102 | * |
||
103 | * @param string $entity |
||
104 | * @param string $type |
||
105 | * @return \Carbon\Carbon $lastSync |
||
106 | */ |
||
107 | private function createCurrentSync($entity, $type) |
||
118 | |||
119 | /** |
||
120 | * Get last sync date from repository. (Or try to...) |
||
121 | * |
||
122 | * @param string|bool $lastSync |
||
123 | * @param string $entity |
||
124 | * @param string $function |
||
125 | * @return mixed |
||
126 | */ |
||
127 | private function getLastSync($lastSync, $entity, $function, $format = 'Y-m-d H:i:s') |
||
149 | |||
150 | /** |
||
151 | * Process a closure inside a try statement |
||
152 | * |
||
153 | * @param string $entity |
||
154 | * @param mixed $lastSync |
||
155 | * @param Closure $try |
||
156 | * @return mixed |
||
157 | */ |
||
158 | private function process($entity, $lastSync, $function, Closure $try) |
||
177 | |||
178 | /** |
||
179 | * Update a build |
||
180 | * |
||
181 | * @param array $response |
||
182 | * @return array |
||
183 | */ |
||
184 | private function updateBuild(Array $response) |
||
195 | |||
196 | /** |
||
197 | * Update a failed build |
||
198 | * |
||
199 | * @param \Exception $e |
||
200 | * @return boolean |
||
201 | */ |
||
202 | private function updateFailedBuild(Exception $e) |
||
215 | |||
216 | /** |
||
217 | * Build from database |
||
218 | * |
||
219 | * @param string $entity |
||
220 | * @param mixed $lastSync |
||
221 | * @return array |
||
222 | */ |
||
223 | private function buildFromDatabase($entity, $lastSync) |
||
233 | |||
234 | /** |
||
235 | * Add options to response |
||
236 | * |
||
237 | * @param array $response |
||
238 | * @return array |
||
239 | */ |
||
240 | private function addRequestOptions(Array $response) |
||
247 | |||
248 | /** |
||
249 | * Receive data from ERP and send it to the repository |
||
250 | * |
||
251 | * Receive all system data. |
||
252 | * |
||
253 | * @param string $entity |
||
254 | * @param mixed $lastSync |
||
255 | * @return array |
||
256 | */ |
||
257 | public function fromErpToDatabase($entity, $lastSync = null) |
||
273 | |||
274 | /** |
||
275 | * Send data from repository to ERP |
||
276 | * |
||
277 | * Usually send orders that was received from device. |
||
278 | * |
||
279 | * @param string $entity |
||
280 | * @param mixed $lastSync |
||
281 | * @return array |
||
282 | */ |
||
283 | public function fromDatabaseToErp($entity, $lastSync = null) |
||
296 | |||
297 | /** |
||
298 | * Send data to Api |
||
299 | * |
||
300 | * Send all Device data. |
||
301 | * |
||
302 | * @param string $entity |
||
303 | * @param mixed $lastSync |
||
304 | * @return array |
||
305 | */ |
||
306 | public function fromDatabaseToApi($entity, $lastSync = null) |
||
319 | |||
320 | /** |
||
321 | * Receive data from Api |
||
322 | * |
||
323 | * Usually receive orders from the device. |
||
324 | * |
||
325 | * @param array $data |
||
326 | * @param string $entity |
||
327 | * @param mixed $lastSync |
||
328 | * @return array |
||
329 | */ |
||
330 | public function fromApiToDatabase(Array $data, $entity, $lastSync = null) |
||
344 | |||
345 | } |
||
346 |
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.