We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 69 | 
| Total Lines | 437 | 
| Duplicated Lines | 0 % | 
| Changes | 6 | ||
| 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  | 
            ||
| 12 | trait Create  | 
            ||
| 13 | { | 
            ||
| 14 | /*  | 
            ||
| 15 | |--------------------------------------------------------------------------  | 
            ||
| 16 | | CREATE  | 
            ||
| 17 | |--------------------------------------------------------------------------  | 
            ||
| 18 | */  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * Insert a row in the database.  | 
            ||
| 22 | *  | 
            ||
| 23 | * @param array $data All input values to be inserted.  | 
            ||
| 24 | *  | 
            ||
| 25 | * @return \Illuminate\Database\Eloquent\Model  | 
            ||
| 26 | */  | 
            ||
| 27 | public function create($data)  | 
            ||
| 28 |     { | 
            ||
| 29 | $data = $this->decodeJsonCastedAttributes($data);  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 30 | $data = $this->compactFakeFields($data);  | 
            ||
| 31 | |||
| 32 | // omit the n-n relationships when updating the eloquent item  | 
            ||
| 33 | $relationships = Arr::pluck($this->getRelationFields(), 'name');  | 
            ||
| 34 | |||
| 35 | // init and fill model  | 
            ||
| 36 | $item = $this->model->make(Arr::except($data, $relationships));  | 
            ||
| 37 | |||
| 38 | // handle BelongsTo 1:1 relations  | 
            ||
| 39 | $item = $this->associateOrDissociateBelongsToRelations($item, $data);  | 
            ||
| 40 | $item->save();  | 
            ||
| 41 | |||
| 42 | // if there are any other relations create them.  | 
            ||
| 43 | $this->createRelations($item, $data);  | 
            ||
| 44 | |||
| 45 | return $item;  | 
            ||
| 46 | }  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * Get all fields needed for the ADD NEW ENTRY form.  | 
            ||
| 50 | *  | 
            ||
| 51 | * @return array The fields with attributes and fake attributes.  | 
            ||
| 52 | */  | 
            ||
| 53 | public function getCreateFields()  | 
            ||
| 54 |     { | 
            ||
| 55 | return $this->fields();  | 
            ||
| 56 | }  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * Get all fields with relation set (model key set on field).  | 
            ||
| 60 | *  | 
            ||
| 61 | * @return array The fields with model key set.  | 
            ||
| 62 | */  | 
            ||
| 63 | public function getRelationFields()  | 
            ||
| 83 | }  | 
            ||
| 84 | |||
| 85 | /**  | 
            ||
| 86 | * Get all fields with n-n relation set (pivot table is true).  | 
            ||
| 87 | *  | 
            ||
| 88 | * @return array The fields with n-n relationships.  | 
            ||
| 89 | */  | 
            ||
| 90 | public function getRelationFieldsWithPivot()  | 
            ||
| 91 |     { | 
            ||
| 92 | $all_relation_fields = $this->getRelationFields();  | 
            ||
| 93 | |||
| 94 |         return Arr::where($all_relation_fields, function ($value, $key) { | 
            ||
| 95 | return isset($value['pivot']) && $value['pivot'];  | 
            ||
| 96 | });  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * Create the relations for the current model.  | 
            ||
| 101 | *  | 
            ||
| 102 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model.  | 
            ||
| 103 | * @param array $data The form data.  | 
            ||
| 104 | */  | 
            ||
| 105 | public function createRelations($item, $data)  | 
            ||
| 109 | }  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Sync the declared many-to-many associations through the pivot field.  | 
            ||
| 113 | *  | 
            ||
| 114 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model.  | 
            ||
| 115 | * @param array $data The form data.  | 
            ||
| 116 | */  | 
            ||
| 117 | public function syncPivot($model, $data)  | 
            ||
| 170 | }  | 
            ||
| 171 | }  | 
            ||
| 172 | }  | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Create any existing one to one relations and subsquent relations for the item.  | 
            ||
| 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 and subsquent relations from form 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)  | 
            ||
| 248 | }  | 
            ||
| 249 | }  | 
            ||
| 250 | }  | 
            ||
| 251 | |||
| 252 | /**  | 
            ||
| 253 | * Associate the nested HasOne -> BelongsTo relations by adding the "connecting key"  | 
            ||
| 254 | * to the array of values that is going to be saved with HasOne relation.  | 
            ||
| 255 | *  | 
            ||
| 256 | * @param array $belongsToRelations  | 
            ||
| 257 | * @param array $modelValues  | 
            ||
| 258 | * @param Model $modelInstance  | 
            ||
| 259 | * @return array  | 
            ||
| 260 | */  | 
            ||
| 261 | private function associateHasOneBelongsTo($belongsToRelations, $modelValues, $modelInstance)  | 
            ||
| 262 |     { | 
            ||
| 263 |         foreach ($belongsToRelations as $methodName => $values) { | 
            ||
| 264 |             $relation = $modelInstance->{$methodName}(); | 
            ||
| 265 | $modelValues[$relation->getForeignKeyName()] = $values['values'][$methodName];  | 
            ||
| 266 | }  | 
            ||
| 267 | |||
| 268 | return $modelValues;  | 
            ||
| 269 | }  | 
            ||
| 270 | |||
| 271 | /**  | 
            ||
| 272 | * Get a relation data array from the form data.  | 
            ||
| 273 | * For each relation defined in the fields through the entity attribute, set the model, the parent model and the  | 
            ||
| 274 | * attribute values.  | 
            ||
| 275 | *  | 
            ||
| 276 | * We traverse this relation array later to create the relations, for example:  | 
            ||
| 277 | *  | 
            ||
| 278 | * Current model HasOne Address, this Address (line_1, country_id) BelongsTo Country through country_id in Address Model.  | 
            ||
| 279 | *  | 
            ||
| 280 | * So when editing current model crud user have two fields address.line_1 and address.country (we infer country_id from relation)  | 
            ||
| 281 | *  | 
            ||
| 282 | * Those will be nested accordingly in this relation array, so address relation will have a nested relation with country.  | 
            ||
| 283 | *  | 
            ||
| 284 | *  | 
            ||
| 285 | * @param array $data The form data.  | 
            ||
| 286 | *  | 
            ||
| 287 | * @return array The formatted relation data.  | 
            ||
| 288 | */  | 
            ||
| 289 | private function getRelationDataFromFormData($data)  | 
            ||
| 290 |     { | 
            ||
| 291 | // exclude the already attached belongs to relations but include nested belongs to.  | 
            ||
| 292 |         $relation_fields = Arr::where($this->getRelationFields(), function ($field, $key) { | 
            ||
| 293 | return $field['relation_type'] !== 'BelongsTo' || $this->isNestedRelation($field);  | 
            ||
| 294 | });  | 
            ||
| 295 | |||
| 296 | $relationData = [];  | 
            ||
| 297 | |||
| 298 |         foreach ($relation_fields as $relation_field) { | 
            ||
| 299 | $attributeKey = $this->parseRelationFieldNamesFromHtml([$relation_field])[0]['name'];  | 
            ||
| 300 |             if (isset($relation_field['pivot']) && $relation_field['pivot'] !== true) { | 
            ||
| 301 |                 $key = implode('.relations.', explode('.', $this->getOnlyRelationEntity($relation_field))); | 
            ||
| 302 | $fieldData = Arr::get($relationData, 'relations.'.$key, []);  | 
            ||
| 303 |                 if (! array_key_exists('model', $fieldData)) { | 
            ||
| 304 | $fieldData['model'] = $relation_field['model'];  | 
            ||
| 305 | }  | 
            ||
| 306 |                 if (! array_key_exists('parent', $fieldData)) { | 
            ||
| 307 | $fieldData['parent'] = $this->getRelationModel($attributeKey, -1);  | 
            ||
| 308 | }  | 
            ||
| 309 | |||
| 310 | // when using HasMany/MorphMany if fallback_id is provided instead of deleting the models  | 
            ||
| 311 | // from database we resort to this fallback provided by developer  | 
            ||
| 312 |                 if (array_key_exists('fallback_id', $relation_field)) { | 
            ||
| 313 | $fieldData['fallback_id'] = $relation_field['fallback_id'];  | 
            ||
| 314 | }  | 
            ||
| 315 | |||
| 316 | // when using HasMany/MorphMany and column is nullable, by default backpack sets the value to null.  | 
            ||
| 317 | // this allow developers to override that behavior and force deletion from database  | 
            ||
| 318 | $fieldData['force_delete'] = $relation_field['force_delete'] ?? false;  | 
            ||
| 319 | |||
| 320 |                 if (! array_key_exists('relation_type', $fieldData)) { | 
            ||
| 321 | $fieldData['relation_type'] = $relation_field['relation_type'];  | 
            ||
| 322 | }  | 
            ||
| 323 |                 $relatedAttribute = Arr::last(explode('.', $attributeKey)); | 
            ||
| 324 | $fieldData['values'][$relatedAttribute] = Arr::get($data, $attributeKey);  | 
            ||
| 325 | |||
| 326 | Arr::set($relationData, 'relations.'.$key, $fieldData);  | 
            ||
| 327 | }  | 
            ||
| 328 | }  | 
            ||
| 329 | |||
| 330 | return $relationData;  | 
            ||
| 331 | }  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * Return the relation without any model attributes there.  | 
            ||
| 335 | * Eg. user.entity_id would return user, as entity_id is not a relation in user.  | 
            ||
| 336 | *  | 
            ||
| 337 | * @param array $relation_field  | 
            ||
| 338 | * @return string  | 
            ||
| 339 | */  | 
            ||
| 340 | public function getOnlyRelationEntity($relation_field)  | 
            ||
| 341 |     { | 
            ||
| 342 |         $entity_array = explode('.', $relation_field['entity']); | 
            ||
| 343 | |||
| 344 | $relation_model = $this->getRelationModel($relation_field['entity'], -1);  | 
            ||
| 345 | |||
| 346 | $related_method = Arr::last($entity_array);  | 
            ||
| 347 | |||
| 348 |         if (! method_exists($relation_model, $related_method)) { | 
            ||
| 349 |             if (count($entity_array) <= 1) { | 
            ||
| 350 | return $relation_field['entity'];  | 
            ||
| 351 |             } else { | 
            ||
| 352 | array_pop($entity_array);  | 
            ||
| 353 | }  | 
            ||
| 354 | |||
| 355 |             return implode('.', $entity_array); | 
            ||
| 356 | }  | 
            ||
| 357 | |||
| 358 | return $relation_field['entity'];  | 
            ||
| 359 | }  | 
            ||
| 360 | |||
| 361 | /**  | 
            ||
| 362 | * When using the HasMany/MorphMany relations as selectable elements we use this function to sync those relations.  | 
            ||
| 363 | * Here we allow for different functionality than when creating. Developer could use this relation as a  | 
            ||
| 364 | * selectable list of items that can belong to one/none entity at any given time.  | 
            ||
| 365 | *  | 
            ||
| 366 | * @return void  | 
            ||
| 367 | */  | 
            ||
| 368 | public function attachManyRelation($item, $relation, $relationMethod, $relationData, $relation_values)  | 
            ||
| 413 | }  | 
            ||
| 414 | }  | 
            ||
| 415 | }  | 
            ||
| 416 | }  | 
            ||
| 417 | |||
| 418 | /**  | 
            ||
| 419 | * Handle HasMany/MorphMany relations when used as creatable entries in the crud.  | 
            ||
| 420 | * By using repeatable field, developer can allow the creation of such entries  | 
            ||
| 421 | * in the crud forms.  | 
            ||
| 422 | *  | 
            ||
| 423 | * @return void  | 
            ||
| 424 | */  | 
            ||
| 425 | public function createManyEntries($entry, $relation, $relationMethod, $relationData)  | 
            ||
| 455 |