We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 50 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 3 | Features | 2 |
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 |
||
| 8 | trait Update |
||
| 9 | { |
||
| 10 | /* |
||
| 11 | |-------------------------------------------------------------------------- |
||
| 12 | | UPDATE |
||
| 13 | |-------------------------------------------------------------------------- |
||
| 14 | */ |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Update a row in the database. |
||
| 18 | * |
||
| 19 | * @param int $id The entity's id |
||
| 20 | * @param array $input All inputs to be updated. |
||
| 21 | * @return object |
||
| 22 | */ |
||
| 23 | public function update($id, $input) |
||
| 24 | { |
||
| 25 | $item = $this->model->findOrFail($id); |
||
| 26 | |||
| 27 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input); |
||
|
|
|||
| 28 | $updated = $item->update($directInputs); |
||
| 29 | $this->createRelationsForItem($item, $relationInputs); |
||
| 30 | |||
| 31 | return $item; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get all fields needed for the EDIT ENTRY form. |
||
| 36 | * |
||
| 37 | * @param int $id The id of the entry that is being edited. |
||
| 38 | * @return array The fields with attributes, fake attributes and values. |
||
| 39 | */ |
||
| 40 | public function getUpdateFields($id = false) |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the value of the 'name' attribute from the declared relation model in the given field. |
||
| 63 | * |
||
| 64 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
| 65 | * @param array $field The CRUD field array. |
||
| 66 | * @return mixed The value of the 'name' attribute from the relation model. |
||
| 67 | */ |
||
| 68 | private function getModelAttributeValue($model, $field) |
||
| 69 | { |
||
| 70 | $model = $model->withFakes(); |
||
| 71 | |||
| 72 | $fieldEntity = $field['entity'] ?? false; |
||
| 73 | $fakeField = $field['fake'] ?? false; |
||
| 74 | |||
| 75 | if ($fieldEntity && ! $fakeField) { |
||
| 76 | return $this->getModelAttributeValueFromRelationship($model, $field); |
||
| 77 | } |
||
| 78 | |||
| 79 | if (is_string($field['name'])) { |
||
| 80 | return $model->{$field['name']}; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (is_array($field['name'])) { |
||
| 84 | $result = []; |
||
| 85 | foreach ($field['name'] as $name) { |
||
| 86 | $result[] = $model->{$name}; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $result; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the value of the given attribute in the relationship. |
||
| 95 | * It takes into account nested relationships. |
||
| 96 | * |
||
| 97 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 98 | * @param array $field |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | private function getModelAttributeValueFromRelationship($model, $field) |
||
| 102 | { |
||
| 103 | [$relatedModel, $relationMethod] = $this->getModelAndMethodFromEntity($model, $field); |
||
| 104 | |||
| 105 | if (! method_exists($relatedModel, $relationMethod)) { |
||
| 106 | return $relatedModel->{$relationMethod}; |
||
| 107 | } |
||
| 108 | |||
| 109 | $relation = $relatedModel->{$relationMethod}(); |
||
| 110 | $relationType = Str::afterLast(get_class($relation), '\\'); |
||
| 111 | |||
| 112 | switch ($relationType) { |
||
| 113 | case 'MorphMany': |
||
| 114 | case 'HasMany': |
||
| 115 | case 'BelongsToMany': |
||
| 116 | case 'MorphToMany': |
||
| 117 | $relationModels = $relatedModel->{$relationMethod}; |
||
| 118 | $result = collect(); |
||
| 119 | |||
| 120 | foreach ($relationModels as $model) { |
||
| 121 | $model = $this->setupRelatedModelLocale($model); |
||
| 122 | // when subfields are NOT set we don't need to get any more values |
||
| 123 | // we just return the plain models as we only need the ids |
||
| 124 | if (! isset($field['subfields'])) { |
||
| 125 | $result->push($model); |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | // when subfields are set we need to parse their values so they can be displayed |
||
| 129 | switch ($relationType) { |
||
| 130 | case 'HasMany': |
||
| 131 | case 'MorphMany': |
||
| 132 | // we will get model direct attributes and merge with subfields values. |
||
| 133 | $directAttributes = $this->getModelWithFakes($model)->getAttributes(); |
||
| 134 | $result->push(array_merge($directAttributes, $this->getSubfieldsValues($field['subfields'], $model))); |
||
| 135 | break; |
||
| 136 | |||
| 137 | case 'BelongsToMany': |
||
| 138 | case 'MorphToMany': |
||
| 139 | // for any given model, we grab the attributes that belong to our pivot table. |
||
| 140 | $item = $model->{$relation->getPivotAccessor()}->getAttributes(); |
||
| 141 | $item[$relationMethod] = $model->getKey(); |
||
| 142 | $result->push($item); |
||
| 143 | break; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return $result; |
||
| 148 | break; |
||
| 149 | case 'HasOne': |
||
| 150 | case 'MorphOne': |
||
| 151 | if (! method_exists($relatedModel, $relationMethod)) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | |||
| 155 | $model = $relatedModel->{$relationMethod}; |
||
| 156 | |||
| 157 | if (! $model) { |
||
| 158 | return; |
||
| 159 | } |
||
| 160 | |||
| 161 | $model = $this->setupRelatedModelLocale($model); |
||
| 162 | $model = $this->getModelWithFakes($model); |
||
| 163 | |||
| 164 | // if `entity` contains a dot here it means developer added a main HasOne/MorphOne relation with dot notation |
||
| 165 | if (Str::contains($field['entity'], '.')) { |
||
| 166 | return $model->{Str::afterLast($field['entity'], '.')}; |
||
| 167 | } |
||
| 168 | |||
| 169 | // when subfields exists developer used the repeatable interface to manage this relation |
||
| 170 | if ($field['subfields']) { |
||
| 171 | return [$this->getSubfieldsValues($field['subfields'], $model)]; |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this->getModelWithFakes($model); |
||
| 175 | |||
| 176 | break; |
||
| 177 | case 'BelongsTo': |
||
| 178 | if ($relatedModel->{$relationMethod}) { |
||
| 179 | return $relatedModel->{$relationMethod}->getKey(); |
||
| 180 | } |
||
| 181 | |||
| 182 | return $relatedModel->{$relationMethod}; |
||
| 183 | break; |
||
| 184 | default: |
||
| 185 | return $relatedModel->{$relationMethod}; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the locale on the related models. |
||
| 191 | * |
||
| 192 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 193 | * @return \Illuminate\Database\Eloquent\Model |
||
| 194 | */ |
||
| 195 | private function setupRelatedModelLocale($model) |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * This function checks if the provided model uses the CrudTrait. |
||
| 209 | * If IT DOES it adds the fakes to the model attributes. |
||
| 210 | * Otherwise just return the model back. |
||
| 211 | * |
||
| 212 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 213 | * @return \Illuminate\Database\Eloquent\Model |
||
| 214 | */ |
||
| 215 | private function getModelWithFakes($model) |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the model and the method from the relation string starting from the provided model. |
||
| 226 | * |
||
| 227 | * @param Illuminate\Database\Eloquent\Model $model |
||
| 228 | * @param array $field |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | private function getModelAndMethodFromEntity($model, $field) |
||
| 232 | { |
||
| 233 | // HasOne and MorphOne relations contains the field in the relation string. We want only the relation part. |
||
| 234 | $relationEntity = $this->getOnlyRelationEntity($field); |
||
| 235 | |||
| 236 | $relationArray = explode('.', $relationEntity); |
||
| 237 | |||
| 238 | $relatedModel = array_reduce(array_splice($relationArray, 0, -1), function ($obj, $method) { |
||
| 239 | // if the string ends with `_id` we strip it out |
||
| 240 | $method = Str::endsWith($method, '_id') ? Str::replaceLast('_id', '', $method) : $method; |
||
| 241 | |||
| 242 | return $obj->{$method} ? $obj->{$method} : $obj; |
||
| 243 | }, $model); |
||
| 244 | |||
| 245 | $relationMethod = Str::afterLast($relationEntity, '.'); |
||
| 246 | |||
| 247 | return [$relatedModel, $relationMethod]; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Return the subfields values from the related model. |
||
| 252 | * |
||
| 253 | * @param array $subfields |
||
| 254 | * @param \Illuminate\Database\Eloquent\Model $relatedModel |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | private function getSubfieldsValues($subfields, $relatedModel) |
||
| 292 | } |
||
| 293 | |||
| 294 | public function hideDeleteButton(): void |
||
| 295 | { |
||
| 299 |