1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace OfflineAgency\MongoAutoSync\Traits; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
|
11
|
|
|
trait MainMongoTrait |
12
|
|
|
{ |
13
|
|
|
protected $has_partial_request; |
14
|
|
|
protected $request; |
15
|
|
|
protected $target_additional_data; |
16
|
|
|
protected $partial_generated_request; |
17
|
|
|
protected $options; |
18
|
|
|
protected $tempEM; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Request $request |
22
|
|
|
* @param array $additionalData |
23
|
|
|
* @param array $options |
24
|
|
|
* @param array $target_additional_data |
25
|
|
|
* @return $this |
26
|
|
|
* @throws Exception |
27
|
|
|
*/ |
28
|
|
|
public function storeWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = []) |
29
|
|
|
{ |
30
|
|
|
$this->initDataForSync($request, $additionalData, $options, $target_additional_data); |
31
|
|
|
$this->storeEditAllItems($request, 'add', $options); |
|
|
|
|
32
|
|
|
$this->processAllRelationships($request, 'add', '', '', $options); |
|
|
|
|
33
|
|
|
|
34
|
|
|
//Dispatch the creation event |
35
|
|
|
$this->fireModelEvent('storeWithSync'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
return $this->fresh(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Request $request |
42
|
|
|
* @param array $additionalData |
43
|
|
|
* @param array $options |
44
|
|
|
* @param array $target_additional_data |
45
|
|
|
* @return $this |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
|
|
public function updateWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = []) |
49
|
|
|
{ |
50
|
|
|
$this->initDataForSync($request, $additionalData, $options, $target_additional_data); |
51
|
|
|
$this->storeEditAllItems($request, 'update', $options); |
52
|
|
|
$this->processAllRelationships($request, 'update', '', '', $options); |
53
|
|
|
|
54
|
|
|
//Dispatch the update event |
55
|
|
|
$this->fireModelEvent('updateWithSync'); |
56
|
|
|
|
57
|
|
|
return $this->fresh(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function destroyWithSync() |
64
|
|
|
{ |
65
|
|
|
//Get the relation info |
66
|
|
|
$relations = $this->getMongoRelation(); |
|
|
|
|
67
|
|
|
|
68
|
|
|
//Process all relationships |
69
|
|
|
foreach ($relations as $method => $relation) { |
70
|
|
|
//Get Relation Save Mode |
71
|
|
|
$type = $relation['type']; |
72
|
|
|
$hasTarget = hasTarget($relation); |
73
|
|
|
if ($hasTarget) { |
74
|
|
|
$modelTarget = $relation['modelTarget']; |
75
|
|
|
$methodOnTarget = $relation['methodOnTarget']; |
76
|
|
|
$modelOnTarget = $relation['modelOnTarget']; |
|
|
|
|
77
|
|
|
|
78
|
|
|
$is_EO = is_EO($type); |
79
|
|
|
$is_EM = is_EM($type); |
80
|
|
|
$is_HO = is_HO($type); |
|
|
|
|
81
|
|
|
$is_HM = is_HM($type); |
|
|
|
|
82
|
|
|
|
83
|
|
|
if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create |
84
|
|
|
//Delete EmbedsMany or EmbedsOne on Target |
85
|
|
|
$this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//TODO: Need to be implemented |
89
|
|
|
/* elseif ($is_HM) {//HasMany |
90
|
|
|
} elseif ($is_HO) {//HasOne Create |
91
|
|
|
}*/ |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
//Delete current object |
95
|
|
|
$this->delete(); |
|
|
|
|
96
|
|
|
|
97
|
|
|
//Dispatch the destroy event |
98
|
|
|
$this->fireModelEvent('destroyWithSync'); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $options |
105
|
|
|
* @param string $key |
106
|
|
|
* @return bool|mixed |
107
|
|
|
*/ |
108
|
|
|
private function getOptionValue(array $options, string $key) |
109
|
|
|
{ |
110
|
|
|
return Arr::has($options, $key) ? $options[$key] : ''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $obj |
115
|
|
|
* @param string $EOkey |
116
|
|
|
* @param string $method |
117
|
|
|
* @param string $model |
118
|
|
|
* @throws Exception |
119
|
|
|
*/ |
120
|
|
|
public function checkPropertyExistence($obj, string $EOkey, $method = '', $model = '') |
121
|
|
|
{ |
122
|
|
|
if (! property_exists($obj, $EOkey)) { |
123
|
|
|
$msg = 'Error - '.$EOkey.' attribute not found on obj '.json_encode($obj).' during save of model: '.$model.' and attribute: '.$method; |
124
|
|
|
throw new Exception($msg); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $arr |
130
|
|
|
* @param string $key |
131
|
|
|
* @throws Exception |
132
|
|
|
*/ |
133
|
|
|
public function checkArrayExistence($arr, string $key) |
134
|
|
|
{ |
135
|
|
|
if (! Arr::has($arr, $key)) { |
136
|
|
|
$msg = ('Error - '.$key.' attribute not found on obj '.json_encode($arr)); |
137
|
|
|
throw new Exception($msg); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Request $request |
143
|
|
|
* @param string $key |
144
|
|
|
* @throws Exception |
145
|
|
|
*/ |
146
|
|
|
private function checkRequestExistence(Request $request, string $key) |
147
|
|
|
{ |
148
|
|
|
if (! $request->has($key)) { |
149
|
|
|
$msg = ('Error - '.$key.' attribute not found in Request '.json_encode($request->all())); |
150
|
|
|
throw new Exception($msg); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param bool $request_has_key |
156
|
|
|
* @param bool $hasTarget |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function getIsSkippable($request_has_key, $hasTarget = false) |
160
|
|
|
{ |
161
|
|
|
return ! $request_has_key && $this->getHasPartialRequest() && ! $hasTarget; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function getHasPartialRequest() |
168
|
|
|
{ |
169
|
|
|
return $this->has_partial_request; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function setHasPartialRequest(): void |
173
|
|
|
{ |
174
|
|
|
$this->has_partial_request = $this->getOptionValue( |
175
|
|
|
$this->getOptions(), |
176
|
|
|
'request_type' |
177
|
|
|
) == 'partial'; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $modelTarget |
182
|
|
|
* @param stdClass $obj |
183
|
|
|
* @return MDModel |
|
|
|
|
184
|
|
|
* @throws Exception |
185
|
|
|
*/ |
186
|
|
|
private function getModelTobeSync(string $modelTarget, stdClass $obj) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$this->checkPropertyExistence($obj, 'ref_id'); |
189
|
|
|
$target_id = $obj->ref_id; |
190
|
|
|
|
191
|
|
|
//Init the Target Model |
192
|
|
|
$modelToBeSync = new $modelTarget; |
193
|
|
|
|
194
|
|
|
return $modelToBeSync->find($target_id); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $key |
199
|
|
|
* @param Request $request |
200
|
|
|
* @return mixed |
201
|
|
|
* @throws Exception |
202
|
|
|
*/ |
203
|
|
|
private function getRelationshipRequest(string $key, Request $request) |
204
|
|
|
{ |
205
|
|
|
$this->checkRequestExistence( |
206
|
|
|
$request, |
207
|
|
|
$key |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
return $request->input($key); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return Request |
215
|
|
|
*/ |
216
|
|
|
public function getRequest() |
217
|
|
|
{ |
218
|
|
|
return $this->request; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param Request $request |
223
|
|
|
* @param array $additionalData |
224
|
|
|
*/ |
225
|
|
|
public function setRequest(Request $request, array $additionalData): void |
226
|
|
|
{ |
227
|
|
|
$request = $request->merge($additionalData); |
228
|
|
|
$this->request = $request; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return Request |
233
|
|
|
*/ |
234
|
|
|
public function getPartialGeneratedRequest() |
235
|
|
|
{ |
236
|
|
|
return $this->partial_generated_request; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param array $arr |
241
|
|
|
*/ |
242
|
|
|
public function setPartialGeneratedRequest(array $arr): void |
243
|
|
|
{ |
244
|
|
|
$partial_generated_request = new Request; |
245
|
|
|
$partial_generated_request->merge($arr); |
246
|
|
|
|
247
|
|
|
$this->partial_generated_request = $partial_generated_request; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
public function getOptions() |
254
|
|
|
{ |
255
|
|
|
return $this->options; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param array $options |
260
|
|
|
*/ |
261
|
|
|
public function setOptions(array $options): void |
262
|
|
|
{ |
263
|
|
|
$this->options = $options; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function getTargetAdditionalData() |
270
|
|
|
{ |
271
|
|
|
return $this->target_additional_data; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param array $target_additional_data |
276
|
|
|
*/ |
277
|
|
|
public function setTargetAdditionalData($target_additional_data): void |
278
|
|
|
{ |
279
|
|
|
$this->target_additional_data = $target_additional_data; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param Request $request |
284
|
|
|
* @param array $additionalData |
285
|
|
|
* @param array $options |
286
|
|
|
* @param array $target_additional_data |
287
|
|
|
*/ |
288
|
|
|
public function initDataForSync(Request $request, array $additionalData, array $options, array $target_additional_data) |
289
|
|
|
{ |
290
|
|
|
$this->setRequest($request, $additionalData); |
291
|
|
|
$this->setTargetAdditionalData($target_additional_data); |
292
|
|
|
$this->setOptions($options); |
293
|
|
|
$this->setHasPartialRequest(); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|