We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 46 |
Total Lines | 277 |
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 |
||
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) |
||
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() |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get all fields with n-n relation set (pivot table is true). |
||
84 | * |
||
85 | * @return array The fields with n-n relationships. |
||
86 | */ |
||
87 | public function getRelationFieldsWithPivot() |
||
88 | { |
||
89 | $all_relation_fields = $this->getRelationFields(); |
||
90 | |||
91 | return Arr::where($all_relation_fields, function ($value, $key) { |
||
92 | return isset($value['pivot']) && $value['pivot']; |
||
93 | }); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Create the relations for the current model. |
||
98 | * |
||
99 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
100 | * @param array $data The form data. |
||
101 | */ |
||
102 | public function createRelations($item, $data) |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Sync the declared many-to-many associations through the pivot field. |
||
110 | * |
||
111 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
112 | * @param array $data The form data. |
||
113 | */ |
||
114 | public function syncPivot($model, $data) |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Create any existing one to one relations and subsquent relations for the item. |
||
150 | * |
||
151 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
152 | * @param array $data The form data. |
||
153 | */ |
||
154 | private function createOneToOneRelations($item, $data) |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Create any existing one to one relations and subsquent relations from form data. |
||
162 | * |
||
163 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
164 | * @param array $formattedData The form data. |
||
165 | * |
||
166 | * @return bool|null |
||
167 | */ |
||
168 | private function createRelationsForItem($item, $formattedData) |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | private function associateHasOneBelongsTo($belongsToRelations, $modelValues, $modelInstance) |
||
206 | { |
||
207 | foreach ($belongsToRelations as $methodName => $values) { |
||
208 | $relation = $modelInstance->{$methodName}(); |
||
209 | $modelValues[$relation->getForeignKeyName()] = $values['values'][$methodName]; |
||
210 | } |
||
211 | |||
212 | return $modelValues; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Get a relation data array from the form data. |
||
217 | * For each relation defined in the fields through the entity attribute, set the model, the parent model and the |
||
218 | * attribute values. |
||
219 | * |
||
220 | * We traverse this relation array later to create the relations, for example: |
||
221 | * |
||
222 | * Current model HasOne Address, this Address (line_1, country_id) BelongsTo Country through country_id in Address Model. |
||
223 | * |
||
224 | * So when editing current model crud user have two fields address.line_1 and address.country (we infer country_id from relation) |
||
225 | * |
||
226 | * Those will be nested accordingly in this relation array, so address relation will have a nested relation with country. |
||
227 | * |
||
228 | * |
||
229 | * @param array $data The form data. |
||
230 | * |
||
231 | * @return array The formatted relation data. |
||
232 | */ |
||
233 | private function getRelationDataFromFormData($data) |
||
234 | { |
||
235 | // exclude the already attached belongs to relations but include nested belongs to. |
||
236 | $relation_fields = Arr::where($this->getRelationFields(), function ($field, $key) { |
||
237 | return $field['relation_type'] !== 'BelongsTo' || $this->isNestedRelation($field); |
||
238 | }); |
||
239 | |||
240 | $relationData = []; |
||
241 | |||
242 | foreach ($relation_fields as $relation_field) { |
||
243 | $attributeKey = $this->parseRelationFieldNamesFromHtml([$relation_field])[0]['name']; |
||
244 | if (isset($relation_field['pivot']) && $relation_field['pivot'] !== true) { |
||
245 | $key = implode('.relations.', explode('.', $this->getOnlyRelationEntity($relation_field))); |
||
246 | $fieldData = Arr::get($relationData, 'relations.'.$key, []); |
||
247 | if (! array_key_exists('model', $fieldData)) { |
||
248 | $fieldData['model'] = $relation_field['model']; |
||
249 | } |
||
250 | if (! array_key_exists('parent', $fieldData)) { |
||
251 | $fieldData['parent'] = $this->getRelationModel($attributeKey, -1); |
||
252 | } |
||
253 | |||
254 | if (! array_key_exists('relation_type', $fieldData)) { |
||
255 | $fieldData['relation_type'] = $relation_field['relation_type']; |
||
256 | } |
||
257 | $relatedAttribute = Arr::last(explode('.', $attributeKey)); |
||
258 | $fieldData['values'][$relatedAttribute] = Arr::get($data, $attributeKey); |
||
259 | |||
260 | Arr::set($relationData, 'relations.'.$key, $fieldData); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | return $relationData; |
||
265 | } |
||
266 | |||
267 | public function getOnlyRelationEntity($relation_field) |
||
286 | } |
||
287 | } |
||
288 |