We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 45 |
| Total Lines | 276 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Update 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 Update, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait Update |
||
| 11 | { |
||
| 12 | /* |
||
| 13 | |-------------------------------------------------------------------------- |
||
| 14 | | UPDATE |
||
| 15 | |-------------------------------------------------------------------------- |
||
| 16 | */ |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Update a row in the database. |
||
| 20 | * |
||
| 21 | * @param int $id The entity's id |
||
| 22 | * @param array $input All inputs to be updated. |
||
| 23 | * @return object |
||
| 24 | */ |
||
| 25 | public function update($id, $input) |
||
| 26 | { |
||
| 27 | $item = $this->model->findOrFail($id); |
||
| 28 | |||
| 29 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input); |
||
|
|
|||
| 30 | if ($this->get('update.useDatabaseTransactions') ?? config('backpack.base.useDatabaseTransactions', false)) { |
||
| 31 | return DB::transaction(fn () => $this->updateModelAndRelations($item, $directInputs, $relationInputs)); |
||
| 32 | } |
||
| 33 | |||
| 34 | return $this->updateModelAndRelations($item, $directInputs, $relationInputs); |
||
| 35 | } |
||
| 36 | |||
| 37 | private function updateModelAndRelations(Model $item, array $directInputs, array $relationInputs): Model |
||
| 38 | { |
||
| 39 | $item->update($directInputs); |
||
| 40 | $this->createRelationsForItem($item, $relationInputs); |
||
| 41 | |||
| 42 | return $item; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get all fields needed for the EDIT ENTRY form. |
||
| 47 | * |
||
| 48 | * @param int $id The id of the entry that is being edited. |
||
| 49 | * @return array The fields with attributes, fake attributes and values. |
||
| 50 | */ |
||
| 51 | public function getUpdateFields($id = false) |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get the value of the 'name' attribute from the declared relation model in the given field. |
||
| 74 | * |
||
| 75 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
| 76 | * @param array $field The CRUD field array. |
||
| 77 | * @return mixed The value of the 'name' attribute from the relation model. |
||
| 78 | */ |
||
| 79 | private function getModelAttributeValue($model, $field) |
||
| 80 | { |
||
| 81 | $model = $model->withFakes(); |
||
| 82 | |||
| 83 | $fieldEntity = $field['entity'] ?? false; |
||
| 84 | $fakeField = $field['fake'] ?? false; |
||
| 85 | |||
| 86 | if ($fieldEntity && ! $fakeField) { |
||
| 87 | return $this->getModelAttributeValueFromRelationship($model, $field); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($this->holdsMultipleInputs($field['name'])) { |
||
| 91 | $result = array_map(function ($item) use ($model) { |
||
| 92 | return $model->{$item}; |
||
| 93 | }, explode(',', $field['name'])); |
||
| 94 | |||
| 95 | return $result; |
||
| 96 | } |
||
| 97 | |||
| 98 | return $model->{$field['name']}; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns the value of the given attribute in the relationship. |
||
| 103 | * It takes into account nested relationships. |
||
| 104 | * |
||
| 105 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 106 | * @param array $field |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | private function getModelAttributeValueFromRelationship($model, $field) |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * This function checks if the provided model uses the CrudTrait. |
||
| 200 | * If IT DOES it adds the fakes to the model attributes. |
||
| 201 | * Otherwise just return the model back. |
||
| 202 | * |
||
| 203 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 204 | * @return \Illuminate\Database\Eloquent\Model |
||
| 205 | */ |
||
| 206 | private function getModelWithFakes($model) |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns the model and the method from the relation string starting from the provided model. |
||
| 217 | * |
||
| 218 | * @param Illuminate\Database\Eloquent\Model $model |
||
| 219 | * @param array $field |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | private function getModelAndMethodFromEntity($model, $field) |
||
| 223 | { |
||
| 224 | // HasOne and MorphOne relations contains the field in the relation string. We want only the relation part. |
||
| 225 | $relationEntity = $this->getOnlyRelationEntity($field); |
||
| 226 | |||
| 227 | $relationArray = explode('.', $relationEntity); |
||
| 228 | |||
| 229 | $relatedModel = array_reduce(array_splice($relationArray, 0, -1), function ($obj, $method) { |
||
| 230 | // if the string ends with `_id` we strip it out |
||
| 231 | $method = Str::endsWith($method, '_id') ? Str::replaceLast('_id', '', $method) : $method; |
||
| 232 | |||
| 233 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
| 234 | }, $model); |
||
| 235 | |||
| 236 | $relationMethod = Str::afterLast($relationEntity, '.'); |
||
| 237 | |||
| 238 | return [$relatedModel, $relationMethod]; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return the subfields values from the related model. |
||
| 243 | * |
||
| 244 | * @param array $subfields |
||
| 245 | * @param \Illuminate\Database\Eloquent\Model $relatedModel |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | private function getSubfieldsValues($subfields, $relatedModel) |
||
| 286 | } |
||
| 287 | } |
||
| 288 |