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