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