We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 59 |
Total Lines | 376 |
Duplicated Lines | 0 % |
Changes | 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 |
||
10 | trait Create |
||
11 | { |
||
12 | /* |
||
13 | |-------------------------------------------------------------------------- |
||
14 | | CREATE |
||
15 | |-------------------------------------------------------------------------- |
||
16 | */ |
||
17 | |||
18 | /** |
||
19 | * Insert a row in the database. |
||
20 | * |
||
21 | * @param array $input All input values to be inserted. |
||
22 | * @return Model |
||
23 | */ |
||
24 | public function create($input) |
||
25 | { |
||
26 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input); |
||
|
|||
27 | |||
28 | if ($this->get('create.useDatabaseTransactions') ?? config('backpack.base.useDatabaseTransactions', false)) { |
||
29 | return DB::transaction(fn () => $this->createModelAndRelations($directInputs, $relationInputs)); |
||
30 | } |
||
31 | |||
32 | return $this->createModelAndRelations($directInputs, $relationInputs); |
||
33 | } |
||
34 | |||
35 | private function createModelAndRelations(array $directInputs, array $relationInputs): Model |
||
36 | { |
||
37 | $item = $this->model->create($directInputs); |
||
38 | $this->createRelationsForItem($item, $relationInputs); |
||
39 | |||
40 | return $item; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get all fields needed for the ADD NEW ENTRY form. |
||
45 | * |
||
46 | * @return array The fields with attributes and fake attributes. |
||
47 | */ |
||
48 | public function getCreateFields() |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get all fields with relation set (model key set on field). |
||
55 | * |
||
56 | * @param array $fields |
||
57 | * @return array The fields with model key set. |
||
58 | */ |
||
59 | public function getRelationFields($fields = []) |
||
60 | { |
||
61 | if (empty($fields)) { |
||
62 | $fields = $this->getCleanStateFields(); |
||
63 | } |
||
64 | |||
65 | $relationFields = []; |
||
66 | |||
67 | foreach ($fields as $field) { |
||
68 | if (isset($field['model']) && $field['model'] !== false && $field['entity'] !== false) { |
||
69 | array_push($relationFields, $field); |
||
70 | } |
||
71 | |||
72 | // if a field has an array name AND subfields |
||
73 | // then take those fields into account (check if they have relationships); |
||
74 | // this is done in particular for the checklist_dependency field, |
||
75 | // but other fields could use it too, in the future; |
||
76 | if ($this->holdsMultipleInputs($field['name']) && |
||
77 | isset($field['subfields']) && |
||
78 | is_array($field['subfields'])) { |
||
79 | foreach ($field['subfields'] as $subfield) { |
||
80 | if (isset($subfield['model']) && $subfield['model'] !== false) { |
||
81 | array_push($relationFields, $subfield); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | return $relationFields; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * --------------- |
||
92 | * PRIVATE METHODS |
||
93 | * ---------------. |
||
94 | */ |
||
95 | |||
96 | /** |
||
97 | * Create relations for the provided model. |
||
98 | * |
||
99 | * @param Model $item The current CRUD model. |
||
100 | * @param array $formattedRelations The form data. |
||
101 | * @return bool|null |
||
102 | */ |
||
103 | private function createRelationsForItem($item, $formattedRelations) |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | private function preparePivotAttributesForCreate(array $attributes, BelongsToMany $relation, string|int $relatedItemKey) |
||
188 | { |
||
189 | $attributes[$relation->getForeignPivotKeyName()] = $relatedItemKey; |
||
190 | $attributes[$relation->getRelatedPivotKeyName()] = $attributes[$relation->getRelationName()]; |
||
191 | $pivotKeyName = $attributes['pivot_key_name'] ?? 'id'; |
||
192 | |||
193 | return Arr::except($attributes, [$relation->getRelationName(), 'pivot_key_name', $pivotKeyName]); |
||
194 | } |
||
195 | |||
196 | private function preparePivotAttributesForUpdate(array $attributes, BelongsToMany $relation) |
||
197 | { |
||
198 | $pivotKeyName = $attributes['pivot_key_name'] ?? 'id'; |
||
199 | $attributes[$relation->getRelatedPivotKeyName()] = $attributes[$relation->getRelationName()]; |
||
200 | |||
201 | return Arr::except($attributes, [$relation->getRelationName(), 'pivot_key_name', $pivotKeyName, $relation->getForeignPivotKeyName()]); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Save the attributes of a given HasOne or MorphOne relationship on the |
||
206 | * related entry, create or delete it, depending on what was sent in the form. |
||
207 | * |
||
208 | * For HasOne and MorphOne relationships, the dev might want to a few different things: |
||
209 | * (A) save an attribute on the related entry (eg. passport.number) |
||
210 | * (B) set an attribute on the related entry to NULL (eg. slug.slug) |
||
211 | * (C) save an entire related entry (eg. passport) |
||
212 | * (D) delete the entire related entry (eg. passport) |
||
213 | * |
||
214 | * @param \Illuminate\Database\Eloquent\Relations\HasOne|\Illuminate\Database\Eloquent\Relations\MorphOne $relation |
||
215 | * @param string $relationMethod The name of the relationship method on the main Model. |
||
216 | * @param array $relationDetails Details about that relationship. For example: |
||
217 | * [ |
||
218 | * 'model' => 'App\Models\Passport', |
||
219 | * 'parent' => 'App\Models\Pet', |
||
220 | * 'entity' => 'passport', |
||
221 | * 'attribute' => 'passport', |
||
222 | * 'values' => **THE TRICKY BIT**, |
||
223 | * ] |
||
224 | * @return Model|null |
||
225 | */ |
||
226 | private function createUpdateOrDeleteOneToOneRelation($relation, $relationMethod, $relationDetails) |
||
227 | { |
||
228 | // Let's see which scenario we're treating, depending on the contents of $relationDetails: |
||
229 | // - (A) ['number' => 1315, 'name' => 'Something'] (if passed using a text/number/etc field) |
||
230 | // - (B) ['slug' => null] (if the 'slug' attribute on the 'slug' related entry needs to be cleared) |
||
231 | // - (C) ['passport' => [['number' => 1314, 'name' => 'Something']]] (if passed using a repeatable field) |
||
232 | // - (D) ['passport' => null] (if deleted from the repeatable field) |
||
233 | |||
234 | // Scenario C or D |
||
235 | if (array_key_exists($relationMethod, $relationDetails['values'])) { |
||
236 | $relationMethodValue = $relationDetails['values'][$relationMethod]; |
||
237 | |||
238 | // Scenario D |
||
239 | if (is_null($relationMethodValue) && $relationDetails['entity'] === $relationMethod) { |
||
240 | $relation->first()?->delete(); |
||
241 | |||
242 | return null; |
||
243 | } |
||
244 | |||
245 | // Scenario C (when it's an array inside an array, because it's been added as one item inside a repeatable field) |
||
246 | if (gettype($relationMethodValue) == 'array' && is_multidimensional_array($relationMethodValue)) { |
||
247 | $relationMethodValue = $relationMethodValue[0]; |
||
248 | } |
||
249 | } |
||
250 | // saving process |
||
251 | $input = $relationMethodValue ?? $relationDetails['values']; |
||
252 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input, $relationDetails, $relationMethod); |
||
253 | |||
254 | $item = $relation->updateOrCreate([], $directInputs); |
||
255 | |||
256 | $this->createRelationsForItem($item, $relationInputs); |
||
257 | |||
258 | return $item; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * When using the HasMany/MorphMany relations as selectable elements we use this function to "mimic-sync" in those relations. |
||
263 | * Since HasMany/MorphMany does not have the `sync` method, we manually re-create it. |
||
264 | * Here we add the entries that developer added and remove the ones that are not in the list. |
||
265 | * This removal process happens with the following rules: |
||
266 | * - by default Backpack will behave like a `sync` from M-M relations: it deletes previous entries and add only the current ones. |
||
267 | * - `force_delete` is configurable in the field, it's `true` by default. When false, if connecting column is nullable instead of deleting the row we set the column to null. |
||
268 | * - `fallback_id` could be provided. In this case instead of deleting we set the connecting key to whatever developer gives us. |
||
269 | * |
||
270 | * @return mixed |
||
271 | */ |
||
272 | private function attachManyRelation($item, $relation, $relationDetails, $relationValues) |
||
307 | } |
||
308 | |||
309 | private function handleManyRelationItemRemoval($modelInstance, $removedEntries, $relationDetails, $relationForeignKey) |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Handle HasMany/MorphMany relations when used as creatable entries in the crud. |
||
348 | * By using repeatable field, developer can allow the creation of such entries |
||
349 | * in the crud forms. |
||
350 | * |
||
351 | * @param $entry - eg: story |
||
352 | * @param $relation - eg story HasMany monsters |
||
353 | * @param $relationMethod - eg: monsters |
||
354 | * @param $relationDetails - eg: info about relation including submited values |
||
355 | * @return void |
||
356 | */ |
||
357 | private function createManyEntries($entry, $relation, $relationMethod, $relationDetails) |
||
386 | } |
||
387 | } |
||
389 |