Total Complexity | 66 |
Total Lines | 426 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like MongoSyncTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MongoSyncTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | trait MongoSyncTrait |
||
11 | { |
||
12 | /** |
||
13 | * @param Request $request |
||
14 | * @param array $additionalData |
||
15 | * @param array $options |
||
16 | * @return $this |
||
17 | * @throws Exception |
||
18 | */ |
||
19 | public function storeWithSync(Request $request, array $additionalData = [], array $options = []) |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param Request $request |
||
34 | * @param string $event |
||
35 | * @param array $options |
||
36 | * @throws Exception |
||
37 | */ |
||
38 | public function storeEditAllItems(Request $request, string $event, array $options) |
||
39 | { |
||
40 | //Get the item name |
||
41 | $items = $this->getItems(); |
||
42 | $is_skippable = $this->getIsSkippable($options); |
||
43 | //Current Obj Create |
||
44 | foreach ($items as $key => $item) { |
||
45 | $is_ML = isML($item); |
||
46 | $is_MD = isMD($item); |
||
47 | |||
48 | $is_fillable = isFillable($item, $event); |
||
49 | |||
50 | if ( is_null($request->input($key)) && $is_skippable ) { |
||
51 | continue; |
||
52 | }else{ |
||
53 | $this->checkRequestExistence( |
||
54 | $request, |
||
55 | $key |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | if ($is_fillable) { |
||
60 | if ($is_ML) { |
||
61 | if (is_null($this->$key)) { |
||
62 | $old_value = []; |
||
63 | } else { |
||
64 | $old_value = $this->$key; |
||
65 | } |
||
66 | $this->$key = ml($old_value, $request->input($key)); |
||
67 | } elseif ($is_MD) { |
||
68 | if ($request->input($key) == '' || $request->input($key) == null) { |
||
69 | $this->$key = null; |
||
70 | } else { |
||
71 | $this->$key = new UTCDateTime(new DateTime($request->input($key))); |
||
72 | } |
||
73 | } else { |
||
74 | $this->$key = $request->input($key); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | |||
79 | $this->save(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param Request $request |
||
84 | * @param string $event |
||
85 | * @param string $parent |
||
86 | * @param string $counter |
||
87 | * @param array $options |
||
88 | * @throws Exception |
||
89 | */ |
||
90 | public function processAllRelationships(Request $request, string $event, string $parent, string $counter, array $options) |
||
91 | { |
||
92 | //Get the relation info |
||
93 | $relations = $this->getMongoRelation(); |
||
94 | $is_skippable = $this->getIsSkippable($options); |
||
95 | |||
96 | //Process all relationships |
||
97 | foreach ($relations as $method => $relation) { |
||
98 | //Get Relation Save Mode |
||
99 | $type = $relation['type']; |
||
100 | $model = $relation['model']; |
||
101 | $hasTarget = hasTarget($relation); |
||
102 | if ($hasTarget) { |
||
103 | $modelTarget = $relation['modelTarget']; |
||
104 | $methodOnTarget = $relation['methodOnTarget']; |
||
105 | $modelOnTarget = $relation['modelOnTarget']; |
||
106 | } else { |
||
107 | $modelTarget = ''; |
||
108 | $methodOnTarget = ''; |
||
109 | $modelOnTarget = ''; |
||
110 | } |
||
111 | |||
112 | $is_EO = is_EO($type); |
||
113 | $is_EM = is_EM($type); |
||
114 | |||
115 | $key = $parent.$method.$counter; |
||
116 | $value = $request->input($key); |
||
117 | if (is_null($value) && $is_skippable) { |
||
118 | continue; |
||
119 | }//Skip with partial request type |
||
120 | |||
121 | if (! is_null($value) && ! ($value == '') && ! ($value == '[]')) { |
||
122 | $objs = json_decode($value); |
||
123 | } else { |
||
124 | $objs = getArrayWithEmptyObj($model, $is_EO, $is_EM); |
||
125 | } |
||
126 | |||
127 | if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create |
||
128 | if ($event == 'update') { |
||
129 | |||
130 | //Delete EmbedsMany or EmbedsOne on Target |
||
131 | if ($hasTarget) { |
||
132 | $this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO); |
||
133 | } |
||
134 | //Delete EmbedsMany or EmbedsOne on current object |
||
135 | if ($is_EM) { |
||
136 | $this->$method()->delete(); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if (! empty($objs)) { |
||
141 | $i = 0; |
||
142 | foreach ($objs as $obj) { |
||
143 | $this->processOneEmbededRelationship( |
||
144 | $request, |
||
145 | $obj, |
||
146 | $type, |
||
147 | $model, |
||
148 | $method, |
||
149 | $modelTarget, |
||
150 | $methodOnTarget, |
||
151 | $modelOnTarget, $event, |
||
152 | $hasTarget, |
||
153 | $is_EO, |
||
154 | $is_EM, |
||
155 | $i, |
||
156 | $options); |
||
157 | $i++; |
||
158 | } |
||
159 | } else { |
||
160 | $this->$method = []; |
||
161 | $this->save(); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param Request $request |
||
169 | * @param $methodOnTarget |
||
170 | * @param $modelOnTarget |
||
171 | * @throws Exception |
||
172 | */ |
||
173 | public function updateRelationWithSync(Request $request, $methodOnTarget, $modelOnTarget) |
||
174 | { |
||
175 | $embededModel = new $modelOnTarget; |
||
176 | //Get the item name |
||
177 | $items = $embededModel->getItems(); |
||
178 | $embededObj = $request->input($methodOnTarget); |
||
179 | $embededObj = json_decode($embededObj); |
||
180 | |||
181 | //Current Obj Create |
||
182 | foreach ($items as $key => $item) { |
||
183 | $is_ML = isML($item); |
||
184 | $is_MD = isMD($item); |
||
185 | |||
186 | if ($is_ML) { |
||
187 | $embededModel->$key = ml([], $embededObj->$key); |
||
188 | } elseif ($is_MD) { |
||
189 | if ($embededObj->$key == '' || $embededObj->$key == null) { |
||
190 | $embededModel->$key = null; |
||
191 | } else { |
||
192 | $embededModel->$key = new UTCDateTime(new DateTime($embededObj->$key)); |
||
193 | } |
||
194 | } else { |
||
195 | $embededModel->$key = $embededObj->$key; |
||
196 | } |
||
197 | } |
||
198 | $this->$methodOnTarget()->associate($embededModel); |
||
199 | $this->save(); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param Request $request |
||
204 | * @param $obj |
||
205 | * @param $type |
||
206 | * @param $model |
||
207 | * @param $method |
||
208 | * @param $modelTarget |
||
209 | * @param $methodOnTarget |
||
210 | * @param $modelOnTarget |
||
211 | * @param $event |
||
212 | * @param $hasTarget |
||
213 | * @param $is_EO |
||
214 | * @param $is_EM |
||
215 | * @param $i |
||
216 | * @param $options |
||
217 | * @throws Exception |
||
218 | */ |
||
219 | public function processOneEmbededRelationship(Request $request, $obj, $type, $model, $method, $modelTarget, $methodOnTarget, $modelOnTarget, $event, $hasTarget, $is_EO, $is_EM, $i, $options) |
||
220 | { |
||
221 | //Init the embedone model |
||
222 | $embedObj = new $model; |
||
223 | |||
224 | $EOitems = $embedObj->getItems(); |
||
225 | //Current Obj Create |
||
226 | foreach ($EOitems as $EOkey => $item) { |
||
227 | if (! is_null($obj)) { |
||
228 | $is_ML = isML($item); |
||
229 | $is_MD = isMD($item); |
||
230 | |||
231 | if ($is_ML) { |
||
232 | $embedObj->$EOkey = ml([], $obj->$EOkey); |
||
233 | } elseif ($EOkey == 'updated_at' || $EOkey == 'created_at') { |
||
234 | $embedObj->$EOkey = now(); |
||
235 | } elseif ($is_MD) { |
||
236 | if ($obj->$EOkey == '' || $obj->$EOkey == null) { |
||
237 | $embedObj->$EOkey = null; |
||
238 | } else { |
||
239 | $embedObj->$EOkey = new UTCDateTime(new DateTime($obj->$EOkey)); |
||
240 | } |
||
241 | } else { |
||
242 | $this->checkPropertyExistence($obj, $EOkey); |
||
243 | $embedObj->$EOkey = $obj->$EOkey; |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | //else if($is_EM){//To be implemented} |
||
249 | //else if($is_HM){//To be implemented} |
||
250 | //else if($is_HO){//To be implemented} |
||
251 | |||
252 | //Get counter for embeds many with level > 1 |
||
253 | $counter = getCounterForRelationships($method, $is_EO, $is_EM, $i); |
||
254 | //Check for another Level of Relationship |
||
255 | $embedObj->processAllRelationships($request, $event, $method.'-', $counter, $options); |
||
256 | |||
257 | if ($is_EO) { |
||
258 | $this->$method = $embedObj->attributes; |
||
259 | } else { |
||
260 | $this->$method()->associate($embedObj); |
||
261 | } |
||
262 | $this->save(); |
||
263 | |||
264 | //dd($embedObj, $this); |
||
265 | |||
266 | if ($hasTarget) {//sync Permission to Permissiongroup |
||
267 | $this->checkPropertyExistence($obj, 'ref_id'); |
||
268 | $target_id = $obj->ref_id; |
||
269 | |||
270 | $ref_id = $this->getId(); |
||
271 | |||
272 | $requestToBeSync = getRequestToBeSync($ref_id, $modelOnTarget, $request, $methodOnTarget); |
||
273 | |||
274 | //Init the Target Model |
||
275 | $modelToBeSync = new $modelTarget; |
||
276 | $modelToBeSync = $modelToBeSync->find($target_id); |
||
277 | if (! is_null($modelToBeSync)) { |
||
278 | $modelToBeSync->updateRelationWithSync($requestToBeSync, $methodOnTarget, $modelOnTarget); |
||
279 | //TODO:Sync target on level > 1 |
||
280 | //$modelToBeSync->processAllRelationships($request, $event, $methodOnTarget, $methodOnTarget . "-"); |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param Request $request |
||
287 | * @param array $additionalData |
||
288 | * @param array $options |
||
289 | * @return $this |
||
290 | * @throws Exception |
||
291 | */ |
||
292 | public function updateWithSync(Request $request, array $additionalData = [], array $options) |
||
293 | { |
||
294 | $request = $request->merge($additionalData); |
||
295 | $this->storeEditAllItems($request, 'update', $options); |
||
296 | $this->processAllRelationships($request, 'update', '', '', $options); |
||
297 | |||
298 | //Dispatch the update event |
||
299 | $this->fireModelEvent('updateWithSync'); |
||
300 | |||
301 | return $this; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param $method |
||
306 | * @param $modelTarget |
||
307 | * @param $methodOnTarget |
||
308 | * @param $id |
||
309 | */ |
||
310 | public function deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO) |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param $target_id |
||
327 | * @param $modelTarget |
||
328 | * @param $methodOnTarget |
||
329 | */ |
||
330 | public function handleSubTarget($target_id, $modelTarget, $methodOnTarget) |
||
342 | } |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @return $this |
||
348 | */ |
||
349 | public function destroyWithSync() |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param array $options |
||
391 | * @param string $key |
||
392 | * @return bool|mixed |
||
393 | */ |
||
394 | private function getOptionValue(array $options, string $key){ |
||
395 | return array_key_exists($key, $options) ? $options[$key] : ''; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * @param array $options |
||
400 | * @return bool |
||
401 | */ |
||
402 | private function getIsSkippable(array $options){ |
||
403 | return $this->getOptionValue( |
||
404 | $options, |
||
405 | 'request_type' |
||
406 | ) == 'partial'; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @param $obj |
||
411 | * @param string $EOkey |
||
412 | */ |
||
413 | private function checkPropertyExistence($obj, string $EOkey){ |
||
414 | try { |
||
415 | if (!property_exists($obj, $EOkey)) { |
||
416 | $msg = ('Error - ' . $EOkey . ' attribute not found on obj ' . json_encode($obj)); |
||
417 | (new Exception($msg) ); |
||
418 | } |
||
419 | }catch (Exception $exception){ |
||
420 | $exception->getMessage(); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @param Request $request |
||
426 | * @param string $key |
||
427 | */ |
||
428 | private function checkRequestExistence(Request $request, string $key){//Can be optimized |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 |