We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 47 |
Total Lines | 260 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 8 | Features | 0 |
Complex classes like HandleRepeatableUploads 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 HandleRepeatableUploads, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait HandleRepeatableUploads |
||
13 | { |
||
14 | public bool $handleRepeatableFiles = false; |
||
15 | |||
16 | public ?string $repeatableContainerName = null; |
||
17 | |||
18 | /******************************* |
||
19 | * Setters - fluently configure the uploader |
||
20 | *******************************/ |
||
21 | public function repeats(string $repeatableContainerName): self |
||
22 | { |
||
23 | $this->handleRepeatableFiles = true; |
||
24 | |||
25 | $this->repeatableContainerName = $repeatableContainerName; |
||
26 | |||
27 | return $this; |
||
28 | } |
||
29 | |||
30 | /******************************* |
||
31 | * Getters |
||
32 | *******************************/ |
||
33 | public function getRepeatableContainerName(): ?string |
||
34 | { |
||
35 | return $this->repeatableContainerName; |
||
36 | } |
||
37 | |||
38 | /******************************* |
||
39 | * Default implementation methods |
||
40 | *******************************/ |
||
41 | protected function uploadRepeatableFiles($values, $previousValues, $entry = null) |
||
42 | { |
||
43 | } |
||
44 | |||
45 | protected function handleRepeatableFiles(Model $entry): Model |
||
58 | } |
||
59 | |||
60 | private function uploadRelationshipFiles(Model $entry, mixed $value): Model |
||
61 | { |
||
62 | $modelCount = CRUD::get('uploaded_'.$this->getRepeatableContainerName().'_count'); |
||
63 | $value = $value->slice($modelCount, 1)->toArray(); |
||
64 | |||
65 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
66 | if (array_key_exists($modelCount, $value) && array_key_exists($uploader->getAttributeName(), $value[$modelCount])) { |
||
67 | $entry->{$uploader->getAttributeName()} = $uploader->uploadFiles($entry, $value[$modelCount][$uploader->getAttributeName()]); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return $entry; |
||
72 | } |
||
73 | |||
74 | protected function processRepeatableUploads(Model $entry, Collection $values): Collection |
||
75 | { |
||
76 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
77 | $uploadedValues = $uploader->uploadRepeatableFiles($values->pluck($uploader->getAttributeName())->toArray(), $this->getPreviousRepeatableValues($entry, $uploader)); |
||
78 | |||
79 | $values = $values->map(function ($item, $key) use ($uploadedValues, $uploader) { |
||
80 | $item[$uploader->getAttributeName()] = $uploadedValues[$key] ?? null; |
||
81 | |||
82 | return $item; |
||
83 | }); |
||
84 | } |
||
85 | |||
86 | return $values; |
||
87 | } |
||
88 | |||
89 | private function retrieveRepeatableFiles(Model $entry): Model |
||
110 | } |
||
111 | |||
112 | private function retrieveRepeatableRelationFiles(Model $entry) |
||
113 | { |
||
114 | switch($this->getRepeatableRelationType()) { |
||
115 | case 'BelongsToMany': |
||
116 | $pivotClass = app('crud')->getModel()->{$this->getUploaderSubfield()['baseEntity']}()->getPivotClass(); |
||
117 | $pivotFieldName = 'pivot_'.$this->getAttributeName(); |
||
118 | $connectedEntry = new $pivotClass([$this->getAttributeName() => $entry->$pivotFieldName]); |
||
119 | $entry->{$pivotFieldName} = $this->retrieveFiles($connectedEntry)->{$this->getAttributeName()}; |
||
120 | |||
121 | break; |
||
122 | default: |
||
123 | $entry = $this->retrieveFiles($entry); |
||
124 | } |
||
125 | |||
126 | return $entry; |
||
127 | } |
||
128 | |||
129 | private function getRepeatableRelationType() |
||
130 | { |
||
131 | return $this->getUploaderField()->getAttributes()['relation_type']; |
||
132 | } |
||
133 | |||
134 | private function getUploaderField() |
||
135 | { |
||
136 | return app('crud')->field($this->getRepeatableContainerName()); |
||
137 | } |
||
138 | |||
139 | private function getUploaderSubfield() |
||
142 | } |
||
143 | |||
144 | private function getUploaderFieldSubfields() |
||
147 | } |
||
148 | |||
149 | private function deleteRepeatableFiles(Model $entry): void |
||
150 | { |
||
151 | if ($this->isRelationship) { |
||
152 | switch($this->getRepeatableRelationType()) { |
||
153 | case 'BelongsToMany': |
||
154 | $pivotAttributes = $entry->getAttributes(); |
||
155 | $connectedPivot = $entry->pivotParent->{$this->getRepeatableContainerName()}->where(function ($item) use ($pivotAttributes) { |
||
156 | $itemPivotAttributes = $item->pivot->only(array_keys($pivotAttributes)); |
||
157 | |||
158 | return $itemPivotAttributes === $pivotAttributes; |
||
159 | })->first(); |
||
160 | |||
161 | if (! $connectedPivot) { |
||
162 | return; |
||
163 | } |
||
164 | |||
165 | $files = $connectedPivot->getOriginal()['pivot_'.$this->getAttributeName()]; |
||
166 | |||
167 | if (! $files) { |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | if (is_array($files)) { |
||
172 | foreach ($files as $value) { |
||
173 | $value = Str::start($value, $this->getPath()); |
||
174 | Storage::disk($this->getDisk())->delete($value); |
||
175 | } |
||
176 | |||
177 | return; |
||
178 | } |
||
179 | |||
180 | $value = Str::start($files, $this->getPath()); |
||
181 | Storage::disk($this->getDisk())->delete($value); |
||
182 | |||
183 | return; |
||
184 | } |
||
185 | |||
186 | $this->deleteFiles($entry); |
||
187 | |||
188 | return; |
||
189 | } |
||
190 | |||
191 | $repeatableValues = collect($entry->{$this->getName()}); |
||
192 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) { |
||
193 | if (! $upload->shouldDeleteFiles()) { |
||
194 | continue; |
||
195 | } |
||
196 | $values = $repeatableValues->pluck($upload->getName())->toArray(); |
||
197 | foreach ($values as $value) { |
||
198 | if (! $value) { |
||
199 | continue; |
||
200 | } |
||
201 | |||
202 | if (is_array($value)) { |
||
203 | foreach ($value as $subvalue) { |
||
204 | Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue); |
||
205 | } |
||
206 | |||
207 | continue; |
||
208 | } |
||
209 | |||
210 | Storage::disk($upload->getDisk())->delete($upload->getPath().$value); |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | /******************************* |
||
215 | * Helper methods |
||
216 | *******************************/ |
||
217 | |||
218 | /** |
||
219 | * Given two multidimensional arrays/collections, merge them recursively. |
||
220 | */ |
||
221 | protected function mergeValuesRecursive(array|Collection $array1, array|Collection $array2): array|Collection |
||
222 | { |
||
223 | $merged = $array1; |
||
224 | foreach ($array2 as $key => &$value) { |
||
225 | if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
||
226 | $merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
||
227 | } else { |
||
228 | $merged[$key] = $value; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | return $merged; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Repeatable items send `_order_` parameter in the request. |
||
237 | * This holds the order of the items in the repeatable container. |
||
238 | */ |
||
239 | protected function getFileOrderFromRequest(): array |
||
249 | } |
||
250 | |||
251 | private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array |
||
252 | { |
||
253 | $previousValues = json_decode($entry->getOriginal($uploader->getRepeatableContainerName()), true); |
||
254 | |||
255 | if (! empty($previousValues)) { |
||
256 | $previousValues = array_column($previousValues, $uploader->getName()); |
||
257 | } |
||
258 | |||
259 | return $previousValues ?? []; |
||
260 | } |
||
261 | |||
262 | private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload) |
||
272 | } |
||
273 | } |
||
274 |