We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 49 |
| Total Lines | 291 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Create 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 Create, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait Create |
||
| 10 | { |
||
| 11 | /* |
||
| 12 | |-------------------------------------------------------------------------- |
||
| 13 | | CREATE |
||
| 14 | |-------------------------------------------------------------------------- |
||
| 15 | */ |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Insert a row in the database. |
||
| 19 | * |
||
| 20 | * @param array $data All input values to be inserted. |
||
| 21 | * |
||
| 22 | * @return \Illuminate\Database\Eloquent\Model |
||
| 23 | */ |
||
| 24 | public function create($data) |
||
| 25 | { |
||
| 26 | $data = $this->decodeJsonCastedAttributes($data); |
||
|
|
|||
| 27 | $data = $this->compactFakeFields($data); |
||
| 28 | |||
| 29 | // omit the relationships when initing the model |
||
| 30 | $relationships = Arr::pluck($this->getRelationFields(), 'name'); |
||
| 31 | |||
| 32 | // init and fill model |
||
| 33 | $item = $this->model->make(Arr::except($data, $relationships)); |
||
| 34 | |||
| 35 | // handle BelongsTo 1:1 relations |
||
| 36 | $item = $this->associateOrDissociateBelongsToRelations($item, $data); |
||
| 37 | $item->save(); |
||
| 38 | |||
| 39 | // if there are any other relations create them. |
||
| 40 | $this->createRelations($item, $data); |
||
| 41 | |||
| 42 | return $item; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get all fields needed for the ADD NEW ENTRY form. |
||
| 47 | * |
||
| 48 | * @return array The fields with attributes and fake attributes. |
||
| 49 | */ |
||
| 50 | public function getCreateFields() |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Associate and dissociate BelongsTo relations in the model. |
||
| 57 | * |
||
| 58 | * @param Model $item |
||
| 59 | * @param array $data The form data. |
||
| 60 | * @return Model Model with relationships set up. |
||
| 61 | */ |
||
| 62 | protected function associateOrDissociateBelongsToRelations($item, array $data) |
||
| 63 | { |
||
| 64 | $belongsToFields = $this->getFieldsWithRelationType('BelongsTo'); |
||
| 65 | |||
| 66 | foreach ($belongsToFields as $relationField) { |
||
| 67 | if (method_exists($item, $this->getOnlyRelationEntity($relationField))) { |
||
| 68 | $relatedId = Arr::get($data, $relationField['name']); |
||
| 69 | if (isset($relatedId) && $relatedId !== null) { |
||
| 70 | $related = $relationField['model']::find($relatedId); |
||
| 71 | |||
| 72 | $item->{$this->getOnlyRelationEntity($relationField)}()->associate($related); |
||
| 73 | } else { |
||
| 74 | $item->{$this->getOnlyRelationEntity($relationField)}()->dissociate(); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | return $item; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get all fields with relation set (model key set on field). |
||
| 84 | * |
||
| 85 | * @return array The fields with model key set. |
||
| 86 | */ |
||
| 87 | public function getRelationFields() |
||
| 88 | { |
||
| 89 | $fields = $this->fields(); |
||
| 90 | $relationFields = []; |
||
| 91 | |||
| 92 | foreach ($fields as $field) { |
||
| 93 | if (isset($field['model']) && $field['model'] !== false) { |
||
| 94 | array_push($relationFields, $field); |
||
| 95 | } |
||
| 96 | |||
| 97 | if (isset($field['subfields']) && |
||
| 98 | is_array($field['subfields']) && |
||
| 99 | count($field['subfields'])) { |
||
| 100 | foreach ($field['subfields'] as $subfield) { |
||
| 101 | array_push($relationFields, $subfield); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return $relationFields; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get all fields with n-n relation set (pivot table is true). |
||
| 111 | * |
||
| 112 | * @return array The fields with n-n relationships. |
||
| 113 | */ |
||
| 114 | public function getRelationFieldsWithPivot() |
||
| 115 | { |
||
| 116 | $all_relation_fields = $this->getRelationFields(); |
||
| 117 | |||
| 118 | return Arr::where($all_relation_fields, function ($value, $key) { |
||
| 119 | return isset($value['pivot']) && $value['pivot']; |
||
| 120 | }); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Create the relations for the current model. |
||
| 125 | * |
||
| 126 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 127 | * @param array $data The form data. |
||
| 128 | */ |
||
| 129 | public function createRelations($item, $data) |
||
| 130 | { |
||
| 131 | $this->syncPivot($item, $data); |
||
| 132 | $this->createOneToOneRelations($item, $data); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Sync the declared many-to-many associations through the pivot field. |
||
| 137 | * |
||
| 138 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
| 139 | * @param array $data The form data. |
||
| 140 | */ |
||
| 141 | public function syncPivot($model, $data) |
||
| 142 | { |
||
| 143 | $fields_with_relationships = $this->getRelationFields(); |
||
| 144 | foreach ($fields_with_relationships as $key => $field) { |
||
| 145 | if (isset($field['pivot']) && $field['pivot']) { |
||
| 146 | $values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
||
| 147 | |||
| 148 | // if a JSON was passed instead of an array, turn it into an array |
||
| 149 | if (is_string($values)) { |
||
| 150 | $values = json_decode($values); |
||
| 151 | } |
||
| 152 | |||
| 153 | $relation_data = []; |
||
| 154 | foreach ($values as $pivot_id) { |
||
| 155 | $pivot_data = []; |
||
| 156 | |||
| 157 | if (isset($field['pivotFields'])) { |
||
| 158 | foreach ($field['pivotFields'] as $pivot_field_name) { |
||
| 159 | $pivot_data[$pivot_field_name] = $data[$pivot_field_name][$pivot_id]; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | $relation_data[$pivot_id] = $pivot_data; |
||
| 163 | } |
||
| 164 | |||
| 165 | $model->{$field['name']}()->sync($relation_data); |
||
| 166 | } |
||
| 167 | |||
| 168 | if (isset($field['morph']) && $field['morph'] && isset($data[$field['name']])) { |
||
| 169 | $values = $data[$field['name']]; |
||
| 170 | $model->{$field['name']}()->sync($values); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Create any existing one to one relations for the current model from the form data. |
||
| 177 | * |
||
| 178 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 179 | * @param array $data The form data. |
||
| 180 | */ |
||
| 181 | private function createOneToOneRelations($item, $data) |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Create any existing one to one relations for the current model from the relation data. |
||
| 189 | * |
||
| 190 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 191 | * @param array $formattedData The form data. |
||
| 192 | * |
||
| 193 | * @return bool|null |
||
| 194 | */ |
||
| 195 | private function createRelationsForItem($item, $formattedData) |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Associate the nested HasOne -> BelongsTo relations by adding the "connecting key" |
||
| 233 | * to the array of values that is going to be saved with HasOne relation. |
||
| 234 | * |
||
| 235 | * @param array $belongsToRelations |
||
| 236 | * @param array $modelValues |
||
| 237 | * @param Model $relationInstance |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | private function associateHasOneBelongsTo($belongsToRelations, $modelValues, $modelInstance) |
||
| 241 | { |
||
| 242 | foreach ($belongsToRelations as $methodName => $values) { |
||
| 243 | $relation = $modelInstance->{$methodName}(); |
||
| 244 | |||
| 245 | $modelValues[$relation->getForeignKeyName()] = $values['values'][$methodName]; |
||
| 246 | } |
||
| 247 | |||
| 248 | return $modelValues; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get a relation data array from the form data. |
||
| 253 | * For each relation defined in the fields through the entity attribute, set the model, the parent model and the |
||
| 254 | * attribute values. |
||
| 255 | * |
||
| 256 | * We traverse this relation array later to create the relations, for example: |
||
| 257 | * |
||
| 258 | * Current model HasOne Address, this Address (line_1, country_id) BelongsTo Country through country_id in Address Model. |
||
| 259 | * |
||
| 260 | * So when editing current model crud user have two fields address.line_1 and address.country (we infer country_id from relation) |
||
| 261 | * |
||
| 262 | * Those will be nested accordingly in this relation array, so address relation will have a nested relation with country. |
||
| 263 | * |
||
| 264 | * |
||
| 265 | * @param array $data The form data. |
||
| 266 | * |
||
| 267 | * @return array The formatted relation data. |
||
| 268 | */ |
||
| 269 | private function getRelationDataFromFormData($data) |
||
| 300 | } |
||
| 301 | } |
||
| 302 |