We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 68 |
Total Lines | 408 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 |
||
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 | $data = $this->changeBelongsToNamesFromRelationshipToForeignKey($data); |
||
32 | |||
33 | // omit the n-n relationships when updating the eloquent item |
||
34 | $nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name'); |
||
35 | |||
36 | $item = $this->model->create(Arr::except($data, $nn_relationships)); |
||
37 | |||
38 | // if there are any other relations create them. |
||
39 | $this->createRelations($item, $data); |
||
40 | |||
41 | return $item; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Get all fields needed for the ADD NEW ENTRY form. |
||
46 | * |
||
47 | * @return array The fields with attributes and fake attributes. |
||
48 | */ |
||
49 | public function getCreateFields() |
||
50 | { |
||
51 | return $this->fields(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get all fields with relation set (model key set on field). |
||
56 | * |
||
57 | * @return array The fields with model key set. |
||
58 | */ |
||
59 | public function getRelationFields() |
||
60 | { |
||
61 | $fields = $this->fields(); |
||
62 | $relationFields = []; |
||
63 | |||
64 | foreach ($fields as $field) { |
||
65 | if (isset($field['model']) && $field['model'] !== false) { |
||
66 | array_push($relationFields, $field); |
||
67 | } |
||
68 | |||
69 | if (isset($field['subfields']) && |
||
70 | is_array($field['subfields']) && |
||
71 | count($field['subfields'])) { |
||
72 | foreach ($field['subfields'] as $subfield) { |
||
73 | array_push($relationFields, $subfield); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | return $relationFields; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get all fields with n-n relation set (pivot table is true). |
||
83 | * |
||
84 | * @return array The fields with n-n relationships. |
||
85 | */ |
||
86 | public function getRelationFieldsWithPivot() |
||
87 | { |
||
88 | $all_relation_fields = $this->getRelationFields(); |
||
89 | |||
90 | return Arr::where($all_relation_fields, function ($value, $key) { |
||
91 | return isset($value['pivot']) && $value['pivot']; |
||
92 | }); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Create the relations for the current model. |
||
97 | * |
||
98 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
99 | * @param array $data The form data. |
||
100 | */ |
||
101 | public function createRelations($item, $data) |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Sync the declared many-to-many associations through the pivot field. |
||
114 | * |
||
115 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
116 | * @param array $data The form data. |
||
117 | */ |
||
118 | public function syncPivot($model, $data) |
||
119 | { |
||
120 | $fields_with_relationships = $this->getRelationFieldsWithPivot(); |
||
121 | foreach ($fields_with_relationships as $field) { |
||
122 | $values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
||
123 | |||
124 | // if a JSON was passed instead of an array, turn it into an array |
||
125 | if (is_string($values)) { |
||
126 | $decoded_values = json_decode($values, true); |
||
127 | $values = []; |
||
128 | // array is not multidimensional |
||
129 | if (count($decoded_values) != count($decoded_values, COUNT_RECURSIVE)) { |
||
130 | foreach ($decoded_values as $value) { |
||
131 | $values[] = $value[$field['name']]; |
||
132 | } |
||
133 | } else { |
||
134 | $values = $decoded_values; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | $relation_data = []; |
||
139 | |||
140 | foreach ($values as $pivot_id) { |
||
141 | if ($pivot_id != '') { |
||
142 | $pivot_data = []; |
||
143 | |||
144 | if (isset($field['pivotFields'])) { |
||
145 | // array is not multidimensional |
||
146 | if (count($field['pivotFields']) == count($field['pivotFields'], COUNT_RECURSIVE)) { |
||
147 | foreach ($field['pivotFields'] as $pivot_field_name) { |
||
148 | $pivot_data[$pivot_field_name] = $data[$pivot_field_name][$pivot_id]; |
||
149 | } |
||
150 | } else { |
||
151 | $field_data = json_decode($data[$field['name']], true); |
||
152 | |||
153 | // we grab from the parsed data the specific values for this pivot |
||
154 | $pivot_data = Arr::first($field_data, function ($item) use ($pivot_id, $field) { |
||
155 | return $item[$field['name']] === $pivot_id; |
||
156 | }); |
||
157 | |||
158 | // we remove the relation field from extra pivot data as we already have the relation. |
||
159 | unset($pivot_data[$field['name']]); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | $relation_data[$pivot_id] = $pivot_data; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | $model->{$field['name']}()->sync($relation_data); |
||
168 | |||
169 | if (isset($field['morph']) && $field['morph'] && isset($data[$field['name']])) { |
||
170 | $values = $data[$field['name']]; |
||
171 | $model->{$field['name']}()->sync($values); |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Handles 1-1 and 1-n relations. In case 1-1 it handles subsequent relations in connected models |
||
178 | * For example, a Monster > HasOne Address > BelongsTo a Country. |
||
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) |
||
186 | { |
||
187 | if (! isset($formattedData['relations'])) { |
||
188 | return false; |
||
189 | } |
||
190 | |||
191 | foreach ($formattedData['relations'] as $relationMethod => $relationData) { |
||
192 | if (! isset($relationData['model'])) { |
||
193 | continue; |
||
194 | } |
||
195 | |||
196 | $relation = $item->{$relationMethod}(); |
||
197 | $relation_type = get_class($relation); |
||
198 | |||
199 | switch ($relation_type) { |
||
200 | case HasOne::class: |
||
201 | case MorphOne::class: |
||
202 | |||
203 | // we first check if there are relations of the relation |
||
204 | if (isset($relationData['relations'])) { |
||
205 | // if there are nested relations, we first add the BelongsTo like in main entry |
||
206 | $belongsToRelations = Arr::where($relationData['relations'], function ($relation_data) { |
||
207 | return $relation_data['relation_type'] == 'BelongsTo'; |
||
208 | }); |
||
209 | |||
210 | // adds the values of the BelongsTo relations of this entity to the array of values that will |
||
211 | // be saved at the same time like we do in parent entity belongs to relations |
||
212 | $valuesWithRelations = $this->associateHasOneBelongsTo($belongsToRelations, $relationData['values'], $relation->getModel()); |
||
213 | |||
214 | // remove previously added BelongsTo relations from relation data. |
||
215 | $relationData['relations'] = Arr::where($relationData['relations'], function ($item) { |
||
216 | return $item['relation_type'] != 'BelongsTo'; |
||
217 | }); |
||
218 | |||
219 | $modelInstance = $relation->updateOrCreate([], $valuesWithRelations); |
||
220 | } else { |
||
221 | $modelInstance = $relation->updateOrCreate([], $relationData['values']); |
||
222 | } |
||
223 | break; |
||
224 | |||
225 | case HasMany::class: |
||
226 | case MorphMany::class: |
||
227 | $relation_values = $relationData['values'][$relationMethod]; |
||
228 | |||
229 | if (is_string($relation_values)) { |
||
230 | $relation_values = json_decode($relationData['values'][$relationMethod], true); |
||
231 | } |
||
232 | |||
233 | if ($relation_values === null || count($relation_values) == count($relation_values, COUNT_RECURSIVE)) { |
||
234 | $this->attachManyRelation($item, $relation, $relationMethod, $relationData, $relation_values); |
||
235 | } else { |
||
236 | $this->createManyEntries($item, $relation, $relationMethod, $relationData); |
||
237 | } |
||
238 | break; |
||
239 | } |
||
240 | |||
241 | if (isset($relationData['relations'])) { |
||
242 | $this->createRelationsForItem($modelInstance, ['relations' => $relationData['relations']]); |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Associate the nested HasOne -> BelongsTo relations by adding the "connecting key" |
||
249 | * to the array of values that is going to be saved with HasOne relation. |
||
250 | * |
||
251 | * @param array $belongsToRelations |
||
252 | * @param array $modelValues |
||
253 | * @param Model $relationInstance |
||
254 | * @return array |
||
255 | */ |
||
256 | private function associateHasOneBelongsTo($belongsToRelations, $modelValues, $modelInstance) |
||
257 | { |
||
258 | foreach ($belongsToRelations as $methodName => $values) { |
||
259 | $relation = $modelInstance->{$methodName}(); |
||
260 | |||
261 | $modelValues[$relation->getForeignKeyName()] = $values['values'][$methodName]; |
||
262 | } |
||
263 | |||
264 | return $modelValues; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Get a relation data array from the form data. |
||
269 | * For each relation defined in the fields through the entity attribute, set the model, the parent model and the |
||
270 | * attribute values. |
||
271 | * |
||
272 | * We traverse this relation array later to create the relations, for example: |
||
273 | * |
||
274 | * Current model HasOne Address, this Address (line_1, country_id) BelongsTo Country through country_id in Address Model. |
||
275 | * |
||
276 | * So when editing current model crud user have two fields address.line_1 and address.country (we infer country_id from relation) |
||
277 | * |
||
278 | * Those will be nested accordingly in this relation array, so address relation will have a nested relation with country. |
||
279 | * |
||
280 | * |
||
281 | * @param array $data The form data. |
||
282 | * |
||
283 | * @return array The formatted relation data. |
||
284 | */ |
||
285 | private function getRelationDataFromFormData($data) |
||
286 | { |
||
287 | // exclude the already attached belongs to relations but include nested belongs to. |
||
288 | $relation_fields = Arr::where($this->getRelationFields(), function ($field, $key) { |
||
289 | return $field['relation_type'] !== 'BelongsTo' || $this->isNestedRelation($field); |
||
290 | }); |
||
291 | |||
292 | $relationData = []; |
||
293 | |||
294 | foreach ($relation_fields as $relation_field) { |
||
295 | $attributeKey = $this->parseRelationFieldNamesFromHtml([$relation_field])[0]['name']; |
||
296 | |||
297 | if ((! is_null(Arr::get($data, $attributeKey)) || $this->isNestedRelation($relation_field)) && isset($relation_field['pivot']) && $relation_field['pivot'] !== true) { |
||
298 | $key = implode('.relations.', explode('.', $this->getOnlyRelationEntity($relation_field))); |
||
299 | $fieldData = Arr::get($relationData, 'relations.'.$key, []); |
||
300 | if (! array_key_exists('model', $fieldData)) { |
||
301 | $fieldData['model'] = $relation_field['model']; |
||
302 | } |
||
303 | if (! array_key_exists('parent', $fieldData)) { |
||
304 | $fieldData['parent'] = $this->getRelationModel($attributeKey, -1); |
||
305 | } |
||
306 | |||
307 | // when using HasMany/MorphMany if fallback_id is provided instead of deleting the models |
||
308 | // from database we resort to this fallback provided by developer |
||
309 | if (array_key_exists('fallback_id', $relation_field)) { |
||
310 | $fieldData['fallback_id'] = $relation_field['fallback_id']; |
||
311 | } |
||
312 | |||
313 | // when using HasMany/MorphMany and column is nullable, by default backpack sets the value to null. |
||
314 | // this allow developers to override that behavior and force deletion from database |
||
315 | $fieldData['force_delete'] = $relation_field['force_delete'] ?? false; |
||
316 | |||
317 | if (! array_key_exists('relation_type', $fieldData)) { |
||
318 | $fieldData['relation_type'] = $relation_field['relation_type']; |
||
319 | } |
||
320 | $relatedAttribute = Arr::last(explode('.', $attributeKey)); |
||
321 | $fieldData['values'][$relatedAttribute] = Arr::get($data, $attributeKey); |
||
322 | |||
323 | Arr::set($relationData, 'relations.'.$key, $fieldData); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | return $relationData; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * When using the HasMany/MorphMany relations as selectable elements we use this function to sync those relations. |
||
332 | * Here we allow for different functionality than when creating. Developer could use this relation as a |
||
333 | * selectable list of items that can belong to one/none entity at any given time. |
||
334 | * |
||
335 | * @return void |
||
336 | */ |
||
337 | public function attachManyRelation($item, $relation, $relationMethod, $relationData, $relation_values) |
||
382 | } |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Handle HasMany/MorphMany relations when used as creatable entries in the crud. |
||
389 | * By using repeatable field, developer can allow the creation of such entries |
||
390 | * in the crud forms. |
||
391 | * |
||
392 | * @return void |
||
393 | */ |
||
394 | public function createManyEntries($entry, $relation, $relationMethod, $relationData) |
||
420 | } |
||
421 | } |
||
422 | } |
||
423 | } |
||
424 |