Total Complexity | 55 |
Total Lines | 360 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 | * @return $this |
||
16 | * @throws Exception |
||
17 | */ |
||
18 | public function storeWithSync(Request $request, array $additionalData = []) |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param $request |
||
34 | * @param $event |
||
35 | * @throws Exception |
||
36 | */ |
||
37 | public function storeEditAllItems($request, $event) |
||
38 | { |
||
39 | //Get the item name |
||
40 | $items = $this->getItems(); |
||
41 | //Current Obj Create |
||
42 | foreach ($items as $key => $item) { |
||
43 | $is_ML = isML($item); |
||
44 | $is_MD = isMD($item); |
||
45 | $is_fillable = isFillable($item, $event); |
||
46 | if ($is_fillable) { |
||
47 | if ($is_ML) { |
||
48 | if (is_null($this->$key)) { |
||
49 | $old_value = array(); |
||
50 | } else { |
||
51 | $old_value = $this->$key; |
||
52 | } |
||
53 | $this->$key = ml($old_value, $request->input($key)); |
||
54 | } elseif ($is_MD) { |
||
55 | // dd( new UTCDateTime(new DateTime($request->input($key)))); |
||
56 | if ($request->input($key) == "" || $request->input($key) == null) { |
||
57 | //dd('if'); |
||
58 | $this->$key = null; |
||
59 | } else { |
||
60 | $this->$key = new UTCDateTime(new DateTime($request->input($key))); |
||
61 | } |
||
62 | } else { |
||
63 | $this->$key = $request->input($key); |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | $this->save(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param Request $request |
||
72 | * @param $event |
||
73 | * @param $parent |
||
74 | * @param $counter |
||
75 | * @throws Exception |
||
76 | */ |
||
77 | public function processAllRelationships(Request $request, $event, $parent, $counter) |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param Request $request |
||
142 | * @param $methodOnTarget |
||
143 | * @param $modelOnTarget |
||
144 | * @throws Exception |
||
145 | */ |
||
146 | public function updateRelationWithSync(Request $request, $methodOnTarget, $modelOnTarget) |
||
147 | { |
||
148 | $embededModel = new $modelOnTarget; |
||
149 | //Get the item name |
||
150 | $items = $embededModel->getItems(); |
||
151 | $embededObj = $request->input($methodOnTarget); |
||
152 | $embededObj = json_decode($embededObj); |
||
153 | |||
154 | //Current Obj Create |
||
155 | foreach ($items as $key => $item) { |
||
156 | $is_ML = isML($item); |
||
157 | $is_MD = isMD($item); |
||
158 | |||
159 | if ($is_ML) { |
||
160 | $embededModel->$key = ml(array(), $embededObj->$key); |
||
161 | } elseif ($is_MD) { |
||
162 | if ($embededObj->$key == "" || $embededObj->$key == null) { |
||
163 | $embededModel->$key = null; |
||
164 | } else { |
||
165 | $embededModel->$key = new UTCDateTime(new DateTime($embededObj->$key)); |
||
166 | } |
||
167 | } else { |
||
168 | $embededModel->$key = $embededObj->$key; |
||
169 | } |
||
170 | } |
||
171 | $this->$methodOnTarget()->associate($embededModel); |
||
172 | $this->save(); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param Request $request |
||
177 | * @param $obj |
||
178 | * @param $type |
||
179 | * @param $model |
||
180 | * @param $method |
||
181 | * @param $modelTarget |
||
182 | * @param $methodOnTarget |
||
183 | * @param $modelOnTarget |
||
184 | * @param $event |
||
185 | * @param $hasTarget |
||
186 | * @param $is_EO |
||
187 | * @param $is_EM |
||
188 | * @param $i |
||
189 | * @throws Exception |
||
190 | */ |
||
191 | public function processOneEmbededRelationship(Request $request, $obj, $type, $model, $method, $modelTarget, $methodOnTarget, $modelOnTarget, $event, $hasTarget, $is_EO, $is_EM, $i) |
||
192 | { |
||
193 | //Init the embedone model |
||
194 | $embedObj = new $model; |
||
195 | |||
196 | $EOitems = $embedObj->getItems(); |
||
197 | //Current Obj Create |
||
198 | foreach ($EOitems as $EOkey => $item) { |
||
199 | if (!is_null($obj)) { |
||
200 | $is_ML = isML($item); |
||
201 | $is_MD = isMD($item); |
||
202 | |||
203 | if ($is_ML) { |
||
204 | $embedObj->$EOkey = ml(array(), $obj->$EOkey); |
||
205 | } elseif ($EOkey == "updated_at" || $EOkey == "created_at") { |
||
206 | $embedObj->$EOkey = now(); |
||
207 | } elseif ($is_MD) { |
||
208 | if ($obj->$EOkey == "" || $obj->$EOkey == null) { |
||
209 | //dd('if'); |
||
210 | $embedObj->$EOkey = null; |
||
211 | } else { |
||
212 | $embedObj->$EOkey = new UTCDateTime(new DateTime($obj->$EOkey)); |
||
213 | } |
||
214 | } else { |
||
215 | if (!property_exists($obj, $EOkey)) { |
||
216 | $msg = ('Error - ' . $EOkey . ' attribute not found on obj ' . json_encode($obj)); |
||
217 | (new Exception($msg) ); |
||
218 | } |
||
219 | $embedObj->$EOkey = $obj->$EOkey; |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | //else if($is_EM){//To be implemented} |
||
225 | //else if($is_HM){//To be implemented} |
||
226 | //else if($is_HO){//To be implemented} |
||
227 | |||
228 | //Get counter for embeds many with level > 1 |
||
229 | $counter = getCounterForRelationships($method, $is_EO, $is_EM, $i); |
||
230 | //Check for another Level of Relationship |
||
231 | $embedObj->processAllRelationships($request, $event, $method . "-", $counter); |
||
232 | |||
233 | if ($is_EO) { |
||
234 | $this->$method = $embedObj->attributes; |
||
235 | } else { |
||
236 | $this->$method()->associate($embedObj); |
||
237 | } |
||
238 | $this->save(); |
||
239 | |||
240 | //dd($embedObj, $this); |
||
241 | |||
242 | if ($hasTarget) { |
||
243 | //sync Permission to Permissiongroup |
||
244 | //Init the Target Model |
||
245 | $target_model = new $modelTarget; |
||
246 | |||
247 | if (!property_exists($obj, 'ref_id')) { |
||
248 | $msg = ('Error - ref_id attribute not found on obj ' . json_encode($obj)); |
||
249 | throw (new \Exception($msg) ); |
||
250 | } |
||
251 | |||
252 | $target_id = $obj->ref_id; |
||
253 | |||
254 | $ref_id = $this->getId(); |
||
255 | |||
256 | $requestToBeSync = getRequestToBeSync($ref_id, $modelOnTarget, $request, $methodOnTarget); |
||
257 | |||
258 | $modelToBeSync = new $modelTarget; |
||
259 | $modelToBeSync = $modelToBeSync->find($target_id); |
||
260 | if (!is_null($modelToBeSync)) { |
||
261 | $modelToBeSync->updateRelationWithSync($requestToBeSync, $methodOnTarget, $modelOnTarget); |
||
262 | //TODO:Sync target on level > 1 |
||
263 | //$modelToBeSync->processAllRelationships($request, $event, $methodOnTarget, $methodOnTarget . "-"); |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | |||
268 | |||
269 | /** |
||
270 | * @param Request $request |
||
271 | * @param array $additionalData |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function updateWithSync(Request $request, array $additionalData = []) |
||
275 | { |
||
276 | $request = $request->merge($additionalData); |
||
277 | $this->storeEditAllItems($request, "update"); |
||
278 | $this->processAllRelationships($request, "update", "", ""); |
||
279 | |||
280 | //Dispatch the update event |
||
281 | $this->fireModelEvent('updateWithSync'); |
||
282 | |||
283 | return $this; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param $method |
||
288 | * @param $modelTarget |
||
289 | * @param $methodOnTarget |
||
290 | * @param $id |
||
291 | */ |
||
292 | public function deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO) |
||
293 | { |
||
294 | if ($is_EO) { |
||
295 | $embedObj = $this->$method; |
||
296 | if (!is_null($embedObj)) { |
||
297 | $target_id = $embedObj->ref_id; |
||
298 | $this->handleSubTarget($target_id, $modelTarget, $methodOnTarget); |
||
299 | } |
||
300 | } else { |
||
301 | foreach ($this->$method as $target) { |
||
302 | $this->handleSubTarget($target->ref_id, $modelTarget, $methodOnTarget); |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param $target_id |
||
309 | * @param $modelTarget |
||
310 | * @param $methodOnTarget |
||
311 | */ |
||
312 | public function handleSubTarget($target_id, $modelTarget, $methodOnTarget) |
||
313 | { |
||
314 | $id = $this->getId(); |
||
315 | $target = new $modelTarget; |
||
316 | $target = $target->all()->where('id', $target_id)->first(); |
||
317 | if (!is_null($target)) { |
||
318 | $subTarget = $target->$methodOnTarget()->where('ref_id', $id)->first(); |
||
319 | $temps = $target->$methodOnTarget()->where('ref_id', '!=', $id); |
||
320 | $target->$methodOnTarget()->delete($subTarget); |
||
321 | foreach ($temps as $temp) { |
||
322 | $target->$methodOnTarget()->associate($temp); |
||
323 | $target->save(); |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function destroyWithSync() |
||
370 | } |
||
371 | } |
||
372 |