We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 83 |
Total Lines | 386 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 12 | 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 |
||
13 | trait HandleRepeatableUploads |
||
14 | { |
||
15 | public bool $handleRepeatableFiles = false; |
||
16 | |||
17 | public null|string $repeatableContainerName = null; |
||
18 | |||
19 | /******************************* |
||
20 | * Setters - fluently configure the uploader |
||
21 | *******************************/ |
||
22 | public function repeats(string $repeatableContainerName): self |
||
23 | { |
||
24 | $this->handleRepeatableFiles = true; |
||
25 | |||
26 | $this->repeatableContainerName = $repeatableContainerName; |
||
27 | |||
28 | return $this; |
||
29 | } |
||
30 | |||
31 | /******************************* |
||
32 | * Getters |
||
33 | *******************************/ |
||
34 | public function getRepeatableContainerName(): null|string |
||
35 | { |
||
36 | return $this->repeatableContainerName; |
||
37 | } |
||
38 | |||
39 | /******************************* |
||
40 | * Default implementation methods |
||
41 | *******************************/ |
||
42 | protected function uploadRepeatableFiles($values, $previousValues, $entry = null) |
||
44 | } |
||
45 | |||
46 | protected function handleRepeatableFiles(Model $entry): Model |
||
47 | { |
||
48 | if ($this->isRelationship) { |
||
49 | return $this->processRelationshipRepeatableUploaders($entry); |
||
50 | } |
||
51 | |||
52 | $values = collect(CRUD::getRequest()->get($this->getRepeatableContainerName())); |
||
|
|||
53 | $files = collect(CRUD::getRequest()->file($this->getRepeatableContainerName())); |
||
54 | |||
55 | $value = $this->mergeValuesRecursive($values, $files); |
||
56 | |||
57 | $processedEntryValues = $this->processRepeatableUploads($entry, $value); |
||
58 | |||
59 | if ($this->isFake()) { |
||
60 | $fakeValues = $entry->{$this->getFakeAttribute()} ?? []; |
||
61 | |||
62 | if (is_string($fakeValues)) { |
||
63 | $fakeValues = json_decode($fakeValues, true); |
||
64 | } |
||
65 | |||
66 | $fakeValues[$this->getRepeatableContainerName()] = empty($processedEntryValues) |
||
67 | ? null |
||
68 | : (isset($entry->getCasts()[$this->getFakeAttribute()]) |
||
69 | ? $processedEntryValues |
||
70 | : json_encode($processedEntryValues)); |
||
71 | |||
72 | $entry->{$this->getFakeAttribute()} = isset($entry->getCasts()[$this->getFakeAttribute()]) |
||
73 | ? $fakeValues |
||
74 | : json_encode($fakeValues); |
||
75 | |||
76 | return $entry; |
||
77 | } |
||
78 | |||
79 | $entry->{$this->getRepeatableContainerName()} = empty($processedEntryValues) |
||
80 | ? null |
||
81 | : (isset($entry->getCasts()[$this->getRepeatableContainerName()]) |
||
82 | ? $processedEntryValues |
||
83 | : json_encode($processedEntryValues)); |
||
84 | |||
85 | return $entry; |
||
86 | } |
||
87 | |||
88 | private function processRelationshipRepeatableUploaders(Model $entry) |
||
89 | { |
||
90 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
91 | $entry = $uploader->uploadRelationshipFiles($entry); |
||
92 | } |
||
93 | |||
94 | return $entry; |
||
95 | } |
||
96 | |||
97 | protected function uploadRelationshipFiles(Model $entry): Model |
||
125 | } |
||
126 | |||
127 | protected function getFilesFromEntry(Model $entry) |
||
128 | { |
||
129 | return $entry->getAttribute($this->getAttributeName()); |
||
130 | } |
||
131 | |||
132 | protected function getEntryAttributeValue(Model $entry) |
||
133 | { |
||
134 | return $entry->{$this->getAttributeName()}; |
||
135 | } |
||
136 | |||
137 | protected function getEntryOriginalValue(Model $entry) |
||
138 | { |
||
139 | return $entry->getOriginal($this->getAttributeName()); |
||
140 | } |
||
141 | |||
142 | protected function shouldUploadFiles($entryValue): bool |
||
143 | { |
||
144 | return true; |
||
145 | } |
||
146 | |||
147 | protected function shouldKeepPreviousValueUnchanged(Model $entry, $entryValue): bool |
||
148 | { |
||
149 | return $entry->exists && ($entryValue === null || $entryValue === [null]); |
||
150 | } |
||
151 | |||
152 | protected function hasDeletedFiles($entryValue): bool |
||
155 | } |
||
156 | |||
157 | protected function processRepeatableUploads(Model $entry, Collection $values): array |
||
158 | { |
||
159 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
160 | $uploadedValues = $uploader->uploadRepeatableFiles($values->pluck($uploader->getAttributeName())->toArray(), $this->getPreviousRepeatableValues($entry, $uploader)); |
||
161 | |||
162 | $values = $values->map(function ($item, $key) use ($uploadedValues, $uploader) { |
||
163 | $item[$uploader->getAttributeName()] = $uploadedValues[$key] ?? null; |
||
164 | |||
165 | return $item; |
||
166 | }); |
||
167 | } |
||
168 | |||
169 | return $values->toArray(); |
||
170 | } |
||
171 | |||
172 | private function retrieveRepeatableFiles(Model $entry): Model |
||
173 | { |
||
174 | if ($this->isRelationship) { |
||
175 | return $this->retrieveRepeatableRelationFiles($entry); |
||
176 | } |
||
177 | |||
178 | $repeatableUploaders = app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()); |
||
179 | |||
180 | if ($this->attachedToFakeField) { |
||
181 | $values = $entry->{$this->attachedToFakeField}; |
||
182 | |||
183 | $values = is_string($values) ? json_decode($values, true) : $values; |
||
184 | |||
185 | $values[$this->getAttributeName()] = isset($values[$this->getAttributeName()]) ? $this->getValueWithoutPath($values[$this->getAttributeName()]) : null; |
||
186 | $entry->{$this->attachedToFakeField} = isset($entry->getCasts()[$this->attachedToFakeField]) ? $values : json_encode($values); |
||
187 | |||
188 | return $entry; |
||
189 | } |
||
190 | |||
191 | $values = $entry->{$this->getAttributeName()}; |
||
192 | $values = is_string($values) ? json_decode($values, true) : $values; |
||
193 | $values = array_map(function ($item) use ($repeatableUploaders) { |
||
194 | foreach ($repeatableUploaders as $upload) { |
||
195 | $item[$upload->getAttributeName()] = $this->getValuesWithPathStripped($item, $upload); |
||
196 | } |
||
197 | |||
198 | return $item; |
||
199 | }, $values ?? []); |
||
200 | |||
201 | $entry->{$this->getRepeatableContainerName()} = $values; |
||
202 | |||
203 | return $entry; |
||
204 | } |
||
205 | |||
206 | private function retrieveRepeatableRelationFiles(Model $entry) |
||
207 | { |
||
208 | switch($this->getRepeatableRelationType()) { |
||
209 | case 'BelongsToMany': |
||
210 | case 'MorphToMany': |
||
211 | $pivotClass = app('crud')->getModel()->{$this->getUploaderSubfield()['baseEntity']}()->getPivotClass(); |
||
212 | $pivotFieldName = 'pivot_'.$this->getAttributeName(); |
||
213 | $connectedEntry = new $pivotClass([$this->getAttributeName() => $entry->$pivotFieldName]); |
||
214 | $entry->{$pivotFieldName} = $this->retrieveFiles($connectedEntry)->{$this->getAttributeName()}; |
||
215 | |||
216 | break; |
||
217 | default: |
||
218 | $entry = $this->retrieveFiles($entry); |
||
219 | } |
||
220 | |||
221 | return $entry; |
||
222 | } |
||
223 | |||
224 | private function getRepeatableRelationType() |
||
225 | { |
||
226 | return $this->getUploaderField()->getAttributes()['relation_type']; |
||
227 | } |
||
228 | |||
229 | private function getUploaderField() |
||
230 | { |
||
231 | return app('crud')->field($this->getRepeatableContainerName()); |
||
232 | } |
||
233 | |||
234 | private function getUploaderSubfield() |
||
237 | } |
||
238 | |||
239 | private function getUploaderFieldSubfields() |
||
240 | { |
||
241 | return $this->getUploaderField()->getAttributes()['subfields']; |
||
242 | } |
||
243 | |||
244 | private function deleteRepeatableFiles(Model $entry): void |
||
245 | { |
||
246 | if ($this->isRelationship) { |
||
247 | $this->deleteRelationshipFiles($entry); |
||
248 | |||
249 | return; |
||
250 | } |
||
251 | |||
252 | if ($this->attachedToFakeField) { |
||
253 | $repeatableValues = $entry->{$this->attachedToFakeField}[$this->getRepeatableContainerName()] ?? null; |
||
254 | $repeatableValues = is_string($repeatableValues) ? json_decode($repeatableValues, true) : $repeatableValues; |
||
255 | $repeatableValues = collect($repeatableValues); |
||
256 | } |
||
257 | |||
258 | $repeatableValues ??= collect($entry->{$this->getRepeatableContainerName()}); |
||
259 | |||
260 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) { |
||
261 | if (! $upload->shouldDeleteFiles()) { |
||
262 | continue; |
||
263 | } |
||
264 | $values = $repeatableValues->pluck($upload->getName())->toArray(); |
||
265 | foreach ($values as $value) { |
||
266 | if (! $value) { |
||
267 | continue; |
||
268 | } |
||
269 | |||
270 | if (is_array($value)) { |
||
271 | foreach ($value as $subvalue) { |
||
272 | Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue); |
||
273 | } |
||
274 | |||
275 | continue; |
||
276 | } |
||
277 | |||
278 | Storage::disk($upload->getDisk())->delete($upload->getPath().$value); |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | /******************************* |
||
283 | * Helper methods |
||
284 | *******************************/ |
||
285 | |||
286 | /** |
||
287 | * Given two multidimensional arrays/collections, merge them recursively. |
||
288 | */ |
||
289 | protected function mergeValuesRecursive(array|Collection $array1, array|Collection $array2): array|Collection |
||
290 | { |
||
291 | $merged = $array1; |
||
292 | foreach ($array2 as $key => &$value) { |
||
293 | if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
||
294 | $merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
||
295 | } else { |
||
296 | $merged[$key] = $value; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | return $merged; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Repeatable items send `_order_` parameter in the request. |
||
305 | * This holds the order of the items in the repeatable container. |
||
306 | */ |
||
307 | protected function getFileOrderFromRequest(): array |
||
317 | } |
||
318 | |||
319 | private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array |
||
320 | { |
||
321 | $previousValues = $entry->getOriginal($uploader->getRepeatableContainerName()); |
||
322 | |||
323 | if (! is_array($previousValues)) { |
||
324 | $previousValues = json_decode($previousValues, true); |
||
325 | } |
||
326 | |||
327 | if (! empty($previousValues)) { |
||
328 | $previousValues = array_column($previousValues, $uploader->getName()); |
||
329 | } |
||
330 | |||
331 | return $previousValues ?? []; |
||
332 | } |
||
333 | |||
334 | private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $uploader) |
||
335 | { |
||
336 | $uploadedValues = $item[$uploader->getName()] ?? null; |
||
337 | if (is_array($uploadedValues)) { |
||
338 | return array_map(function ($value) use ($uploader) { |
||
339 | return $uploader->getValueWithoutPath($value); |
||
340 | }, $uploadedValues); |
||
341 | } |
||
342 | |||
343 | return isset($uploadedValues) ? $uploader->getValueWithoutPath($uploadedValues) : null; |
||
344 | } |
||
345 | |||
346 | private function deleteRelationshipFiles(Model $entry): void |
||
347 | { |
||
348 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
349 | $uploader->deleteRepeatableRelationFiles($entry); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | private function deleteRepeatableRelationFiles(Model $entry) |
||
399 | } |
||
400 | } |
||
401 |