1
|
|
|
<?php namespace Algorit\Synchronizer\Storage; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
|
5
|
|
|
final class SyncEloquentRepository implements SyncRepositoryInterface { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The entity instance. |
9
|
|
|
* |
10
|
|
|
* @var \Algorit\Synchronizer\Storage\Sync |
11
|
|
|
*/ |
12
|
|
|
protected $entity; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The current instance. |
16
|
|
|
* |
17
|
|
|
* @var object |
18
|
|
|
*/ |
19
|
|
|
protected $current; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Define the entity instance |
23
|
|
|
* |
24
|
|
|
* @return void |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public function __construct(Sync $entity) |
27
|
|
|
{ |
28
|
|
|
$this->entity = $entity; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new model. |
33
|
|
|
* |
34
|
|
|
* @return Collection |
35
|
|
|
*/ |
36
|
|
|
public function create($data) |
37
|
|
|
{ |
38
|
|
|
return $this->entity->create($data); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Update the fillable data given an instance. |
43
|
|
|
* |
44
|
|
|
* @return Collection |
45
|
|
|
*/ |
46
|
|
|
public function update($instance, $data) |
47
|
|
|
{ |
48
|
|
|
if($instance == false) |
49
|
|
|
{ |
50
|
|
|
return array( |
|
|
|
|
51
|
|
|
'response' => false |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$fillable = $this->entity->getFillable(); |
56
|
|
|
|
57
|
|
|
$updated = array(); |
58
|
|
|
$skipped = array(); |
59
|
|
|
|
60
|
|
|
foreach($data as $key => $value) |
61
|
|
|
{ |
62
|
|
|
if( ! in_array($key, $fillable)) |
63
|
|
|
{ |
64
|
|
|
$skipped[$key] = $value; |
65
|
|
|
|
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$updated[$key] = $value; |
70
|
|
|
|
71
|
|
|
$instance->$key = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return array( |
|
|
|
|
75
|
|
|
'response' => $instance->save(), |
76
|
|
|
'updated' => $updated, |
77
|
|
|
'skipped' => $skipped |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get last sync date |
83
|
|
|
* |
84
|
|
|
* @param mixed $resource |
85
|
|
|
* @param string $entity |
86
|
|
|
* @param string $type |
87
|
|
|
* @return \Algorit\Synchronizer\Storage\Sync |
88
|
|
|
*/ |
89
|
|
|
public function getLastSync($resource, $entity, $type) |
90
|
|
|
{ |
91
|
|
|
$last = $this->entity->where('status', 'success') |
|
|
|
|
92
|
|
|
->where('entity', $entity) |
93
|
|
|
->where('type', $type); |
94
|
|
|
|
95
|
|
|
return $last->orderBy('created_at', 'DESC')->first(array('created_at')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the current sync instance |
100
|
|
|
* |
101
|
|
|
* @param \Algorit\Synchronizer\Storage\Sync $instance |
102
|
|
|
* @return \Algorit\Synchronizer\Storage\Sync |
103
|
|
|
*/ |
104
|
|
|
public function setCurrentSync($instance) |
105
|
|
|
{ |
106
|
|
|
return $this->current = $instance; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the current sync |
111
|
|
|
* |
112
|
|
|
* @param void |
113
|
|
|
* @return \Algorit\Synchronizer\Storage\Sync |
114
|
|
|
*/ |
115
|
|
|
public function getCurrentSync() |
116
|
|
|
{ |
117
|
|
|
return $this->current; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Update the current sync |
122
|
|
|
* |
123
|
|
|
* @param array $data |
124
|
|
|
* @return \Algorit\Synchronizer\Storage\Sync |
125
|
|
|
*/ |
126
|
|
|
public function updateCurrentSync(Array $data) |
127
|
|
|
{ |
128
|
|
|
return $this->update($this->current, $data); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Touch the current sync timestamps. |
133
|
|
|
* |
134
|
|
|
* @param void |
135
|
|
|
* @return \Algorit\Synchronizer\Storage\Sync |
136
|
|
|
*/ |
137
|
|
|
public function touchCurrentSync() |
138
|
|
|
{ |
139
|
|
|
return $this->current->touch(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Update the current sync using an exception |
144
|
|
|
* |
145
|
|
|
* @param \Exception $exception |
146
|
|
|
* @return \Algorit\Synchronizer\Storage\Sync |
147
|
|
|
*/ |
148
|
|
|
public function updateFailedSync(Exception $exception) |
149
|
|
|
{ |
150
|
|
|
return $this->updateCurrentSync(array( |
|
|
|
|
151
|
|
|
'status' => 'fail', |
152
|
|
|
'response' => json_encode(array( |
153
|
|
|
'exception' => get_class($exception), |
154
|
|
|
'message' => $exception->getMessage(), |
155
|
|
|
'code' => $exception->getCode(), |
156
|
|
|
'file' => $exception->getFile(), |
157
|
|
|
'line' => $exception->getLine() |
158
|
|
|
)) |
159
|
|
|
)); |
160
|
|
|
} |
161
|
|
|
} |
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.