| Conditions | 6 |
| Paths | 10 |
| Total Lines | 148 |
| Code Lines | 124 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 45 | * @throws Exception |
||
| 46 | */ |
||
| 47 | public function setMiniModels() |
||
| 48 | { |
||
| 49 | $miniModelList = $this->getUniqueMiniModelList(); |
||
| 50 | $this->mini_models = $this->populateMiniModels($miniModelList); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | public function getMiniModels() |
||
| 57 | { |
||
| 58 | return $this->mini_models; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return array |
||
| 63 | * |
||
| 64 | * @throws Exception |
||
| 65 | */ |
||
| 66 | public function getUniqueMiniModelList() |
||
| 67 | { |
||
| 68 | $relationships = $this->getMongoRelation(); |
||
| 69 | |||
| 70 | $models = []; |
||
| 71 | $embedded_object = []; |
||
| 72 | |||
| 73 | foreach ($relationships as $method => $relationship) { |
||
| 74 | $hasTarget = hasTarget($relationship); |
||
| 75 | if ($hasTarget) { |
||
| 76 | $relationshipsContainsTarget = Arr::has($relationship, 'modelOnTarget'); |
||
| 77 | if ($relationshipsContainsTarget) { |
||
| 78 | $models[] = Arr::get($relationship, 'modelOnTarget'); |
||
| 79 | $embedded_object[$method] = $this->getObjWithRefId($method, $relationship); |
||
| 80 | } else { |
||
| 81 | throw new Exception('modelOnTarget not found on relationship '.$method.' array. Check your Model configuration '.get_class($this)); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | $this->setPartialGeneratedRequest($embedded_object); |
||
|
|
|||
| 86 | |||
| 87 | return collect($models)->unique()->toArray(); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param array $miniModelList |
||
| 92 | * @return mixed |
||
| 93 | * |
||
| 94 | * @throws Exception |
||
| 95 | */ |
||
| 96 | public function populateMiniModels(array $miniModelList) |
||
| 97 | { |
||
| 98 | $miniModels = []; |
||
| 99 | foreach ($miniModelList as $miniModel) { |
||
| 100 | $miniModels[$miniModel] = $this->getFreshMiniModel($miniModel); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $miniModels; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $mini_model_path |
||
| 108 | * @return MDModel |
||
| 109 | * |
||
| 110 | * @throws Exception |
||
| 111 | */ |
||
| 112 | public function getFreshMiniModel(string $mini_model_path) |
||
| 113 | { |
||
| 114 | $embededModel = $this->getModelInstanceFromPath($mini_model_path); |
||
| 115 | $items = $embededModel->getItems(); |
||
| 116 | foreach ($items as $key => $item) { |
||
| 117 | $embededModel->$key = $this->castValueToBeSaved($key, $item, $mini_model_path); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $embededModel; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $key |
||
| 125 | * @param $item |
||
| 126 | * @param string $mini_model_path |
||
| 127 | * @return array|mixed|UTCDateTime|null |
||
| 128 | * |
||
| 129 | * @throws Exception |
||
| 130 | */ |
||
| 131 | public function castValueToBeSaved(string $key, $item, string $mini_model_path) |
||
| 132 | { |
||
| 133 | $is_ML = isML($item); |
||
| 134 | $is_MD = isMD($item); |
||
| 135 | $is_array = $this->isArray($item); |
||
| 136 | $is_carbon_date = $this->isCarbonDate($item); |
||
| 137 | |||
| 138 | $value = $this->getObjValueToBeSaved($key, $mini_model_path); |
||
| 139 | if ($is_ML) { |
||
| 140 | return is_array($value) ? $value : ml([], $value); |
||
| 141 | } elseif ($is_MD) { |
||
| 142 | if ($value instanceof UTCDateTime) { |
||
| 143 | return $value; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($value == '') { |
||
| 147 | return null; |
||
| 148 | } |
||
| 149 | |||
| 150 | return new UTCDateTime(new DateTime($value)); |
||
| 151 | } elseif ($is_carbon_date) { |
||
| 152 | if ($value == '') { |
||
| 153 | return new UTCDateTime(); |
||
| 154 | } |
||
| 155 | |||
| 156 | return new UTCDateTime($value); |
||
| 157 | } elseif ($is_array) { |
||
| 158 | return is_null($value) ? [] : (is_array($value) ? $value : $value->getAttributes()); |
||
| 159 | } else { |
||
| 160 | return $value; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $mini_model_path |
||
| 166 | * @return MDModel |
||
| 167 | */ |
||
| 168 | public function getModelInstanceFromPath(string $mini_model_path) |
||
| 169 | { |
||
| 170 | return new $mini_model_path; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $key |
||
| 175 | * @param string $mini_model_path |
||
| 176 | * @param bool $rewrite_ref_id_key |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function getObjValueToBeSaved(string $key, string $mini_model_path, $rewrite_ref_id_key = true) |
||
| 180 | { |
||
| 181 | $key = $key === 'ref_id' && $rewrite_ref_id_key ? '_id' : $key; |
||
| 182 | $target_additional_data = $this->getTargetAdditionalData(); |
||
| 183 | $request = $this->getRequest(); |
||
| 184 | |||
| 185 | $db_value = $this->getDbValue($key); |
||
| 186 | |||
| 187 | return Arr::has($target_additional_data, $mini_model_path.'.'.$key) ? Arr::get($target_additional_data, $mini_model_path.'.'.$key) : // Search on target_additional_data [] 4th parameter of updateWithSync() / storeWithSync() |
||
| 188 | ($request->has($key) ? $request->input($key) : $db_value); // Search on Main Request 1st parameter of updateWithSync() / storeWithSync() or directly on database |
||
| 189 | //TODO: Add default value from Item Model |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $key |
||
| 250 |