We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 49 |
Total Lines | 307 |
Duplicated Lines | 0 % |
Changes | 30 | ||
Bugs | 10 | Features | 5 |
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 |
||
7 | trait Create |
||
8 | { |
||
9 | /* |
||
10 | |-------------------------------------------------------------------------- |
||
11 | | CREATE |
||
12 | |-------------------------------------------------------------------------- |
||
13 | */ |
||
14 | |||
15 | /** |
||
16 | * Insert a row in the database. |
||
17 | * |
||
18 | * @param array $input All input values to be inserted. |
||
19 | * @return \Illuminate\Database\Eloquent\Model |
||
20 | */ |
||
21 | public function create($input) |
||
22 | { |
||
23 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input); |
||
|
|||
24 | $item = $this->model->create($directInputs); |
||
25 | $this->createRelationsForItem($item, $relationInputs); |
||
26 | |||
27 | return $item; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Get all fields needed for the ADD NEW ENTRY form. |
||
32 | * |
||
33 | * @return array The fields with attributes and fake attributes. |
||
34 | */ |
||
35 | public function getCreateFields() |
||
36 | { |
||
37 | return $this->fields(); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get all fields with relation set (model key set on field). |
||
42 | * |
||
43 | * @param array $fields |
||
44 | * @return array The fields with model key set. |
||
45 | */ |
||
46 | public function getRelationFields($fields = []) |
||
47 | { |
||
48 | if (empty($fields)) { |
||
49 | $fields = $this->getCleanStateFields(); |
||
50 | } |
||
51 | |||
52 | $relationFields = []; |
||
53 | |||
54 | foreach ($fields as $field) { |
||
55 | if (isset($field['model']) && $field['model'] !== false) { |
||
56 | array_push($relationFields, $field); |
||
57 | } |
||
58 | |||
59 | // if a field has an array name AND subfields |
||
60 | // then take those fields into account (check if they have relationships); |
||
61 | // this is done in particular for the checklist_dependency field, |
||
62 | // but other fields could use it too, in the future; |
||
63 | if (is_array($field['name']) && |
||
64 | isset($field['subfields']) && |
||
65 | is_array($field['subfields']) && |
||
66 | count($field['subfields'])) { |
||
67 | foreach ($field['subfields'] as $subfield) { |
||
68 | if (isset($subfield['model']) && $subfield['model'] !== false) { |
||
69 | array_push($relationFields, $subfield); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return $relationFields; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * --------------- |
||
80 | * PRIVATE METHODS |
||
81 | * ---------------. |
||
82 | */ |
||
83 | |||
84 | /** |
||
85 | * Create relations for the provided model. |
||
86 | * |
||
87 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
88 | * @param array $formattedRelations The form data. |
||
89 | * @return bool|null |
||
90 | */ |
||
91 | private function createRelationsForItem($item, $formattedRelations) |
||
92 | { |
||
93 | // no relations to create |
||
94 | if (empty($formattedRelations)) { |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | foreach ($formattedRelations as $relationMethod => $relationDetails) { |
||
99 | $relation = $item->{$relationMethod}(); |
||
100 | $relationType = $relationDetails['relation_type']; |
||
101 | |||
102 | switch ($relationType) { |
||
103 | case 'HasOne': |
||
104 | case 'MorphOne': |
||
105 | $this->createUpdateOrDeleteOneToOneRelation($relation, $relationMethod, $relationDetails); |
||
106 | break; |
||
107 | case 'HasMany': |
||
108 | case 'MorphMany': |
||
109 | $relationValues = $relationDetails['values'][$relationMethod]; |
||
110 | // if relation values are null we can only attach, also we check if we sent |
||
111 | // - a single dimensional array: [1,2,3] |
||
112 | // - an array of arrays: [[1][2][3]] |
||
113 | // if is as single dimensional array we can only attach. |
||
114 | if ($relationValues === null || ! is_multidimensional_array($relationValues)) { |
||
115 | $this->attachManyRelation($item, $relation, $relationDetails, $relationValues); |
||
116 | } else { |
||
117 | $this->createManyEntries($item, $relation, $relationMethod, $relationDetails); |
||
118 | } |
||
119 | break; |
||
120 | case 'BelongsToMany': |
||
121 | case 'MorphToMany': |
||
122 | $values = $relationDetails['values'][$relationMethod] ?? []; |
||
123 | $values = is_string($values) ? json_decode($values, true) : $values; |
||
124 | $relationValues = []; |
||
125 | |||
126 | if (is_array($values) && is_multidimensional_array($values)) { |
||
127 | foreach ($values as $value) { |
||
128 | if (isset($value[$relationMethod])) { |
||
129 | $relationValues[$value[$relationMethod]] = Arr::except($value, $relationMethod); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | // if there is no relation data, and the values array is single dimensional we have |
||
135 | // an array of keys with no aditional pivot data. sync those. |
||
136 | if (empty($relationValues)) { |
||
137 | $relationValues = array_values($values); |
||
138 | } |
||
139 | |||
140 | $item->{$relationMethod}()->sync($relationValues); |
||
141 | break; |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Save the attributes of a given HasOne or MorphOne relationship on the |
||
148 | * related entry, create or delete it, depending on what was sent in the form. |
||
149 | * |
||
150 | * For HasOne and MorphOne relationships, the dev might want to a few different things: |
||
151 | * (A) save an attribute on the related entry (eg. passport.number) |
||
152 | * (B) set an attribute on the related entry to NULL (eg. slug.slug) |
||
153 | * (C) save an entire related entry (eg. passport) |
||
154 | * (D) delete the entire related entry (eg. passport) |
||
155 | * |
||
156 | * @param \Illuminate\Database\Eloquent\Relations\HasOne|\Illuminate\Database\Eloquent\Relations\MorphOne $relation |
||
157 | * @param string $relationMethod The name of the relationship method on the main Model. |
||
158 | * @param array $relationDetails Details about that relationship. For example: |
||
159 | * [ |
||
160 | * 'model' => 'App\Models\Passport', |
||
161 | * 'parent' => 'App\Models\Pet', |
||
162 | * 'entity' => 'passport', |
||
163 | * 'attribute' => 'passport', |
||
164 | * 'values' => **THE TRICKY BIT**, |
||
165 | * ] |
||
166 | * @return Model|null |
||
167 | */ |
||
168 | private function createUpdateOrDeleteOneToOneRelation($relation, $relationMethod, $relationDetails) |
||
169 | { |
||
170 | // Let's see which scenario we're treating, depending on the contents of $relationDetails: |
||
171 | // - (A) ['number' => 1315, 'name' => 'Something'] (if passed using a text/number/etc field) |
||
172 | // - (B) ['slug' => null] (if the 'slug' attribute on the 'slug' related entry needs to be cleared) |
||
173 | // - (C) ['passport' => [['number' => 1314, 'name' => 'Something']]] (if passed using a repeatable field) |
||
174 | // - (D) ['passport' => null] (if deleted from the repeatable field) |
||
175 | |||
176 | // Scenario C or D |
||
177 | if (array_key_exists($relationMethod, $relationDetails['values'])) { |
||
178 | $relationMethodValue = $relationDetails['values'][$relationMethod]; |
||
179 | |||
180 | // Scenario D |
||
181 | if (is_null($relationMethodValue) && $relationDetails['entity'] === $relationMethod) { |
||
182 | $relation->delete(); |
||
183 | |||
184 | return null; |
||
185 | } |
||
186 | |||
187 | // Scenario C (when it's an array inside an array, because it's been added as one item inside a repeatable field) |
||
188 | if (gettype($relationMethodValue) == 'array' && is_multidimensional_array($relationMethodValue)) { |
||
189 | $relationMethodValue = $relationMethodValue[0]; |
||
190 | } |
||
191 | } |
||
192 | // saving process |
||
193 | $input = $relationMethodValue ?? $relationDetails['values']; |
||
194 | [$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($input, $relationDetails, $relationMethod); |
||
195 | |||
196 | $item = $relation->updateOrCreate([], $directInputs); |
||
197 | |||
198 | $this->createRelationsForItem($item, $relationInputs); |
||
199 | |||
200 | return $item; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * When using the HasMany/MorphMany relations as selectable elements we use this function to "mimic-sync" in those relations. |
||
205 | * Since HasMany/MorphMany does not have the `sync` method, we manually re-create it. |
||
206 | * Here we add the entries that developer added and remove the ones that are not in the list. |
||
207 | * This removal process happens with the following rules: |
||
208 | * - by default Backpack will behave like a `sync` from M-M relations: it deletes previous entries and add only the current ones. |
||
209 | * - `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. |
||
210 | * - `fallback_id` could be provided. In this case instead of deleting we set the connecting key to whatever developer gives us. |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | private function attachManyRelation($item, $relation, $relationDetails, $relationValues) |
||
251 | } |
||
252 | |||
253 | private function handleManyRelationItemRemoval($modelInstance, $removedEntries, $relationDetails, $relationForeignKey) |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Handle HasMany/MorphMany relations when used as creatable entries in the crud. |
||
276 | * By using repeatable field, developer can allow the creation of such entries |
||
277 | * in the crud forms. |
||
278 | * |
||
279 | * @param $entry - eg: story |
||
280 | * @param $relation - eg story HasMany monsters |
||
281 | * @param $relationMethod - eg: monsters |
||
282 | * @param $relationDetails - eg: info about relation including submited values |
||
283 | * @return void |
||
284 | */ |
||
285 | private function createManyEntries($entry, $relation, $relationMethod, $relationDetails) |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 |