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 | 267 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | 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 n-n relationships when updating the eloquent item |
||
| 30 | $nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name'); |
||
| 31 | |||
| 32 | // init and fill model |
||
| 33 | $item = $this->model->make(Arr::except($data, $nn_relationships)); |
||
| 34 | |||
| 35 | // handle BelongsTo 1:1 relations |
||
| 36 | $item = $this->associateBelongsToRelations($item, $data); |
||
| 37 | $item->save(); |
||
| 38 | |||
| 39 | // if there are any relationships available, also sync those |
||
| 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() |
||
| 51 | { |
||
| 52 | return $this->fields(); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get all fields with relation set (model key set on field). |
||
| 57 | * |
||
| 58 | * @return array The fields with model key set. |
||
| 59 | */ |
||
| 60 | public function getRelationFields() |
||
| 61 | { |
||
| 62 | $fields = $this->fields(); |
||
| 63 | $relationFields = []; |
||
| 64 | |||
| 65 | foreach ($fields as $field) { |
||
| 66 | if (isset($field['model']) && $field['model'] !== false) { |
||
| 67 | array_push($relationFields, $field); |
||
| 68 | } |
||
| 69 | |||
| 70 | if (isset($field['subfields']) && |
||
| 71 | is_array($field['subfields']) && |
||
| 72 | count($field['subfields'])) { |
||
| 73 | foreach ($field['subfields'] as $subfield) { |
||
| 74 | array_push($relationFields, $subfield); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | return $relationFields; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Associate and dissociate. |
||
| 84 | * |
||
| 85 | * @param Model |
||
| 86 | * @param array The form data. |
||
| 87 | * @return Model Model with relationships set up. |
||
| 88 | */ |
||
| 89 | public function associateBelongsToRelations($item, array $data) |
||
| 90 | { |
||
| 91 | foreach ($this->getFieldsWithRelationType('BelongsTo') as $relationField) { |
||
| 92 | $item->{$this->getOnlyRelationEntity($relationField)}() |
||
| 93 | ->associate($relationField['model']::find(Arr::get($data, $relationField['name']))); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $item; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get all fields with n-n relation set (pivot table is true). |
||
| 101 | * |
||
| 102 | * @return array The fields with n-n relationships. |
||
| 103 | */ |
||
| 104 | public function getRelationFieldsWithPivot() |
||
| 105 | { |
||
| 106 | $all_relation_fields = $this->getRelationFields(); |
||
| 107 | |||
| 108 | return Arr::where($all_relation_fields, function ($value, $key) { |
||
| 109 | return isset($value['pivot']) && $value['pivot']; |
||
| 110 | }); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Create the relations for the current model. |
||
| 115 | * |
||
| 116 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 117 | * @param array $data The form data. |
||
| 118 | */ |
||
| 119 | public function createRelations($item, $data) |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Sync the declared many-to-many associations through the pivot field. |
||
| 127 | * |
||
| 128 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
| 129 | * @param array $data The form data. |
||
| 130 | */ |
||
| 131 | public function syncPivot($model, $data) |
||
| 132 | { |
||
| 133 | $fields_with_relationships = $this->getRelationFields(); |
||
| 134 | foreach ($fields_with_relationships as $key => $field) { |
||
| 135 | if (isset($field['pivot']) && $field['pivot']) { |
||
| 136 | $values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
||
| 137 | |||
| 138 | // if a JSON was passed instead of an array, turn it into an array |
||
| 139 | if (is_string($values)) { |
||
| 140 | $values = json_decode($values); |
||
| 141 | } |
||
| 142 | |||
| 143 | $relation_data = []; |
||
| 144 | foreach ($values as $pivot_id) { |
||
| 145 | $pivot_data = []; |
||
| 146 | |||
| 147 | if (isset($field['pivotFields'])) { |
||
| 148 | foreach ($field['pivotFields'] as $pivot_field_name) { |
||
| 149 | $pivot_data[$pivot_field_name] = $data[$pivot_field_name][$pivot_id]; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | $relation_data[$pivot_id] = $pivot_data; |
||
| 153 | } |
||
| 154 | |||
| 155 | $model->{$field['name']}()->sync($relation_data); |
||
| 156 | } |
||
| 157 | |||
| 158 | if (isset($field['morph']) && $field['morph'] && isset($data[$field['name']])) { |
||
| 159 | $values = $data[$field['name']]; |
||
| 160 | $model->{$field['name']}()->sync($values); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Create any existing one to one relations for the current model from the form data. |
||
| 167 | * |
||
| 168 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 169 | * @param array $data The form data. |
||
| 170 | */ |
||
| 171 | private function createOneToOneRelations($item, $data) |
||
| 172 | { |
||
| 173 | $relationData = $this->getRelationDataFromFormData($data); |
||
| 174 | $this->createRelationsForItem($item, $relationData); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Create any existing one to one relations for the current model from the relation data. |
||
| 179 | * |
||
| 180 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 181 | * @param array $formattedData The form data. |
||
| 182 | * |
||
| 183 | * @return bool|null |
||
| 184 | */ |
||
| 185 | private function createRelationsForItem($item, $formattedData) |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get a relation data array from the form data. |
||
| 215 | * For each relation defined in the fields through the entity attribute, set the model, the parent model and the |
||
| 216 | * attribute values. |
||
| 217 | * |
||
| 218 | * We traverse this relation array later to create the relations, for example: |
||
| 219 | * |
||
| 220 | * Current model HasOne Address, this Address (line_1, country_id) BelongsTo Country through country_id in Address Model. |
||
| 221 | * |
||
| 222 | * So when editing current model crud user have two fields address.line_1 and address.country (we infer country_id from relation) |
||
| 223 | * |
||
| 224 | * Those will be nested accordingly in this relation array, so address relation will have a nested relation with country. |
||
| 225 | * |
||
| 226 | * |
||
| 227 | * @param array $data The form data. |
||
| 228 | * |
||
| 229 | * @return array The formatted relation data. |
||
| 230 | */ |
||
| 231 | private function getRelationDataFromFormData($data) |
||
| 232 | { |
||
| 233 | $relation_fields = $this->getRelationFields(); |
||
| 234 | $relationData = []; |
||
| 235 | foreach ($relation_fields as $relation_field) { |
||
| 236 | $attributeKey = $this->parseRelationFieldNamesFromHtml([$relation_field])[0]['name']; |
||
| 237 | |||
| 238 | if (! is_null(Arr::get($data, $attributeKey)) && isset($relation_field['pivot']) && $relation_field['pivot'] !== true) { |
||
| 239 | $key = implode('.relations.', explode('.', $this->getOnlyRelationEntity($relation_field))); |
||
| 240 | $fieldData = Arr::get($relationData, 'relations.'.$key, []); |
||
| 241 | if (! array_key_exists('model', $fieldData)) { |
||
| 242 | $fieldData['model'] = $relation_field['model']; |
||
| 243 | } |
||
| 244 | if (! array_key_exists('parent', $fieldData)) { |
||
| 245 | $fieldData['parent'] = $this->getRelationModel($attributeKey, -1); |
||
| 246 | } |
||
| 247 | $relatedAttribute = Arr::last(explode('.', $attributeKey)); |
||
| 248 | $fieldData['values'][$relatedAttribute] = Arr::get($data, $attributeKey); |
||
| 249 | |||
| 250 | Arr::set($relationData, 'relations.'.$key, $fieldData); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | return $relationData; |
||
| 255 | } |
||
| 256 | |||
| 257 | public function getOnlyRelationEntity($relation_field) |
||
| 276 | } |
||
| 277 | } |
||
| 278 |