| Conditions | 19 |
| Paths | 316 |
| Total Lines | 97 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function processAllRelationships(Request $request, string $event, string $parent, string $counter, array $options) |
||
| 20 | { |
||
| 21 | $this->setMiniModels(); // For target Sync |
||
|
|
|||
| 22 | |||
| 23 | //Get the relation info |
||
| 24 | $relations = $this->getMongoRelation(); |
||
| 25 | |||
| 26 | //Process all relationships |
||
| 27 | foreach ($relations as $method => $relation) { |
||
| 28 | //Get Relation Save Mode |
||
| 29 | $type = $relation['type']; |
||
| 30 | $model = $relation['model']; |
||
| 31 | $hasTarget = hasTarget($relation); |
||
| 32 | if ($hasTarget) { |
||
| 33 | $modelTarget = $relation['modelTarget']; |
||
| 34 | $methodOnTarget = $relation['methodOnTarget']; |
||
| 35 | $modelOnTarget = $relation['modelOnTarget']; |
||
| 36 | $typeOnTarget = Arr::has($relation, 'typeOnTarget') ? Arr::get($relation, 'typeOnTarget') : 'EmbedsMany'; |
||
| 37 | } else { |
||
| 38 | $modelTarget = ''; |
||
| 39 | $methodOnTarget = ''; |
||
| 40 | $modelOnTarget = ''; |
||
| 41 | $typeOnTarget = ''; |
||
| 42 | } |
||
| 43 | |||
| 44 | $is_EO = is_EO($type); |
||
| 45 | $is_EM = is_EM($type); |
||
| 46 | |||
| 47 | $is_EM_target = is_EM($typeOnTarget); |
||
| 48 | $is_EO_target = is_EO($typeOnTarget); |
||
| 49 | |||
| 50 | $key = $parent.$method.$counter; |
||
| 51 | $is_skippable = $this->getIsSkippable($request->has($key), $hasTarget); |
||
| 52 | |||
| 53 | if ($is_skippable) { |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | $current_request = $request->has($key) ? $request : $this->getPartialGeneratedRequest(); |
||
| 57 | |||
| 58 | $value = $this->getRelationshipRequest($key, $current_request); |
||
| 59 | |||
| 60 | $is_embeds_has_to_be_updated = $request->has($key); |
||
| 61 | |||
| 62 | if (! is_null($value) && ! ($value == '') && ! ($value == '[]')) { |
||
| 63 | $objs = json_decode($value); |
||
| 64 | } else { |
||
| 65 | $objs = getArrayWithEmptyObj($model, $is_EO, $is_EM); |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create |
||
| 69 | if ($event == 'update' && $is_embeds_has_to_be_updated) { |
||
| 70 | |||
| 71 | //Delete EmbedsMany or EmbedsOne on Target - TODO: check if it is necessary to run deleteTargetObj method |
||
| 72 | if ($hasTarget) { |
||
| 73 | $this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EO_target, $is_EM_target); |
||
| 74 | } |
||
| 75 | //Delete EmbedsMany or EmbedsOne on current object |
||
| 76 | if ($is_EM) { |
||
| 77 | $this->$method = []; |
||
| 78 | $this->save(); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if (! empty($objs)) { |
||
| 83 | if ($is_EM) { |
||
| 84 | $this->tempEM = []; |
||
| 85 | } |
||
| 86 | |||
| 87 | $i = 0; |
||
| 88 | foreach ($objs as $obj) { |
||
| 89 | $this->processOneEmbeddedRelationship( |
||
| 90 | $request, |
||
| 91 | $obj, |
||
| 92 | $type, |
||
| 93 | $model, |
||
| 94 | $method, |
||
| 95 | $modelTarget, |
||
| 96 | $methodOnTarget, |
||
| 97 | $modelOnTarget, $event, |
||
| 98 | $hasTarget, |
||
| 99 | $is_EO, |
||
| 100 | $is_EM, |
||
| 101 | $is_EO_target, |
||
| 102 | $is_EM_target, |
||
| 103 | $i, |
||
| 104 | $is_embeds_has_to_be_updated, |
||
| 105 | $options); |
||
| 106 | $i++; |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($is_EM) { |
||
| 110 | $this->$method = $this->tempEM; |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | $this->$method = []; |
||
| 114 | } |
||
| 115 | $this->save(); |
||
| 116 | } |
||
| 299 |