| Total Complexity | 52 |
| Total Lines | 332 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 5 | Features | 2 |
Complex classes like RelationshipMongoTrait 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 RelationshipMongoTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | trait RelationshipMongoTrait |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @param Request $request |
||
| 15 | * @param string $event |
||
| 16 | * @param string $parent |
||
| 17 | * @param string $counter |
||
| 18 | * @param array $options |
||
| 19 | * |
||
| 20 | * @throws Exception |
||
| 21 | */ |
||
| 22 | public function processAllRelationships(Request $request, string $event, string $parent, string $counter, array $options) |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param $mini_model |
||
| 125 | * @param string $method_on_target |
||
| 126 | * @param bool $is_EO_target |
||
| 127 | * @param bool $is_EM_target |
||
| 128 | * |
||
| 129 | * @throws \Throwable |
||
| 130 | */ |
||
| 131 | public function updateRelationWithSync($mini_model, string $method_on_target, $is_EO_target, $is_EM_target) |
||
| 132 | { |
||
| 133 | if ($is_EM_target) { |
||
| 134 | $new_values = []; |
||
| 135 | throw_if( |
||
| 136 | ! isset($this->$method_on_target), |
||
| 137 | new Exception( |
||
| 138 | 'Error during target update. Remember to init the attribute '.$method_on_target. |
||
| 139 | ' on collection '.$this->getCollection() |
||
| 140 | ) |
||
| 141 | ); |
||
| 142 | |||
| 143 | $is_update_operation = false; |
||
| 144 | foreach ($this->$method_on_target as $temp) { |
||
| 145 | throw_if( |
||
| 146 | is_array($temp), |
||
| 147 | new Exception( |
||
| 148 | 'Error during target update. Remember to declare '.$method_on_target.' as '. |
||
| 149 | 'EmbedsMany relationship on model '.get_class($this) |
||
| 150 | ) |
||
| 151 | ); |
||
| 152 | |||
| 153 | if (!is_null($temp)) { |
||
| 154 | if ($this->getHasPartialRequest()) { |
||
| 155 | if (Arr::get($temp->attributes, 'ref_id') !== Arr::get($mini_model->attributes, 'ref_id')) { |
||
| 156 | $new_values[] = $temp->attributes; |
||
| 157 | } else { |
||
| 158 | $new_values[] = $mini_model->attributes; |
||
| 159 | $is_update_operation = true; |
||
| 160 | } |
||
| 161 | } else { |
||
| 162 | $new_values[] = $mini_model->attributes; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | if (!$is_update_operation) { |
||
| 168 | $new_values[] = $mini_model->attributes; |
||
| 169 | } |
||
| 170 | } elseif ($is_EO_target) { |
||
| 171 | throw_if( |
||
| 172 | is_array($mini_model), |
||
| 173 | new Exception( |
||
| 174 | 'Error during target update. Remember to declare '.$method_on_target.' as '. |
||
| 175 | 'EmbedOne relationship on model '.get_class($this) |
||
| 176 | ) |
||
| 177 | ); |
||
| 178 | $new_values = $mini_model->attributes; |
||
| 179 | } |
||
| 180 | |||
| 181 | $this->$method_on_target = $new_values; |
||
| 182 | $this->save(); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param Request $request |
||
| 187 | * @param $obj |
||
| 188 | * @param $type |
||
| 189 | * @param $model |
||
| 190 | * @param $method |
||
| 191 | * @param $modelTarget |
||
| 192 | * @param $methodOnTarget |
||
| 193 | * @param $modelOnTarget |
||
| 194 | * @param $event |
||
| 195 | * @param $hasTarget |
||
| 196 | * @param bool $is_EO |
||
| 197 | * @param bool $is_EM |
||
| 198 | * @param bool $is_EO_target |
||
| 199 | * @param bool $is_EM_target |
||
| 200 | * @param $i |
||
| 201 | * @param bool $is_embeds_has_to_be_updated |
||
| 202 | * @param $options |
||
| 203 | * |
||
| 204 | * @throws Exception |
||
| 205 | */ |
||
| 206 | public function processOneEmbeddedRelationship(Request $request, $obj, $type, $model, $method, $modelTarget, $methodOnTarget, $modelOnTarget, $event, $hasTarget, $is_EO, $is_EM, $is_EO_target, $is_EM_target, $i, $is_embeds_has_to_be_updated, $options) |
||
| 207 | { |
||
| 208 | if ($is_embeds_has_to_be_updated) { |
||
| 209 | $this->processEmbedOnCurrentCollection($request, $obj, $type, $model, $method, $event, $is_EO, $is_EM, $i, $options); |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($hasTarget) { |
||
| 213 | $this->processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, $is_EO_target, $is_EM_target); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $method |
||
| 219 | * @param string $modelTarget |
||
| 220 | * @param string $methodOnTarget |
||
| 221 | * @param bool $is_EO |
||
| 222 | * @param bool $is_EM |
||
| 223 | * @param bool $is_EO_target |
||
| 224 | * @param bool $is_EM_target |
||
| 225 | */ |
||
| 226 | public function deleteTargetObj($method, $modelTarget, $methodOnTarget, bool $is_EO, bool $is_EM, bool $is_EO_target, bool $is_EM_target) |
||
| 227 | { |
||
| 228 | if ($is_EO) { |
||
| 229 | $embedObj = $this->$method; |
||
| 230 | if (! is_null($embedObj)) { |
||
| 231 | $target_id = $embedObj->ref_id; |
||
| 232 | $this->handleSubTarget($target_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target); |
||
| 233 | } |
||
| 234 | } elseif ($is_EM) { |
||
| 235 | foreach ($this->$method as $target) { |
||
| 236 | $this->handleSubTarget($target->ref_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string|null $target_id |
||
| 243 | * @param string $modelTarget |
||
| 244 | * @param string $methodOnTarget |
||
| 245 | * @param bool $is_EO_target |
||
| 246 | * @param bool $is_EM_target |
||
| 247 | */ |
||
| 248 | public function handleSubTarget(?string $target_id, string $modelTarget, string $methodOnTarget, bool $is_EO_target, bool $is_EM_target) |
||
| 264 | //Do nothing because when we are updating we already init the informations |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param Request $request |
||
| 270 | * @param $obj |
||
| 271 | * @param $type |
||
| 272 | * @param $model |
||
| 273 | * @param $method |
||
| 274 | * @param $event |
||
| 275 | * @param $is_EO |
||
| 276 | * @param $is_EM |
||
| 277 | * @param $i |
||
| 278 | * @param $options |
||
| 279 | * |
||
| 280 | * @throws Exception |
||
| 281 | */ |
||
| 282 | private function processEmbedOnCurrentCollection(Request $request, $obj, $type, $model, $method, $event, $is_EO, $is_EM, $i, $options) |
||
| 283 | { |
||
| 284 | //Init the embed one model |
||
| 285 | $embedObj = new $model; |
||
| 286 | |||
| 287 | $EOitems = $embedObj->getItems(); |
||
| 288 | //Current Obj Create |
||
| 289 | foreach ($EOitems as $EOkey => $item) { |
||
| 290 | if (! is_null($obj)) { |
||
| 291 | $is_ML = isML($item); |
||
| 292 | $is_MD = isMD($item); |
||
| 293 | $this->checkPropertyExistence($obj, $EOkey, $method, $model); |
||
| 294 | |||
| 295 | if ($is_ML) { |
||
| 296 | $embedObj->$EOkey = ml([], $obj->$EOkey); |
||
| 297 | } elseif ($EOkey == 'updated_at' || $EOkey == 'created_at') { |
||
| 298 | $embedObj->$EOkey = now(); |
||
| 299 | } elseif ($is_MD) { |
||
| 300 | if ($obj->$EOkey == '' || $obj->$EOkey == null) { |
||
| 301 | $embedObj->$EOkey = null; |
||
| 302 | } else { |
||
| 303 | $embedObj->$EOkey = new UTCDateTime(new DateTime($obj->$EOkey)); |
||
| 304 | } |
||
| 305 | } else { |
||
| 306 | $embedObj->$EOkey = $obj->$EOkey; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | //else if($is_EM){//To be implemented} |
||
| 312 | //else if($is_HM){//To be implemented} |
||
| 313 | //else if($is_HO){//To be implemented} |
||
| 314 | |||
| 315 | //Get counter for embeds many with level > 1 |
||
| 316 | $counter = getCounterForRelationships($method, $is_EO, $is_EM, $i); |
||
| 317 | //Check for another Level of Relationship |
||
| 318 | $embedObj->processAllRelationships($request, $event, $method.'-', $counter, $options); |
||
| 319 | |||
| 320 | if ($is_EO) { |
||
| 321 | $this->$method = $embedObj->attributes; |
||
| 322 | } else { |
||
| 323 | $this->tempEM[] = $embedObj->attributes; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $modelTarget |
||
| 329 | * @param $obj |
||
| 330 | * @param $methodOnTarget |
||
| 331 | * @param $modelOnTarget |
||
| 332 | * @param bool $is_EO_target |
||
| 333 | * @param bool $is_EM_target |
||
| 334 | * |
||
| 335 | * @throws Exception |
||
| 336 | */ |
||
| 337 | private function processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, bool $is_EO_target, bool $is_EM_target) |
||
| 343 | //TODO:Sync target on level > 1 |
||
| 344 | //$modelToBeSync->processAllRelationships($request, $event, $methodOnTarget, $methodOnTarget . "-"); |
||
| 348 |