We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 69 |
Total Lines | 336 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 11 | 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) |
||
43 | { |
||
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 | $value = $this->mergeValuesRecursive($values, $files); |
||
55 | |||
56 | $entry->{$this->getRepeatableContainerName()} = json_encode($this->processRepeatableUploads($entry, $value)); |
||
57 | |||
58 | return $entry; |
||
59 | } |
||
60 | |||
61 | private function processRelationshipRepeatableUploaders(Model $entry) |
||
62 | { |
||
63 | foreach(app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
64 | $entry = $uploader->uploadRelationshipFiles($entry); |
||
65 | } |
||
66 | |||
67 | return $entry; |
||
68 | } |
||
69 | |||
70 | protected function uploadRelationshipFiles(Model $entry): Model |
||
97 | } |
||
98 | |||
99 | protected function getFilesFromEntry(Model $entry) |
||
100 | { |
||
101 | return $entry->getAttribute($this->getAttributeName()); |
||
102 | } |
||
103 | |||
104 | protected function getEntryAttributeValue(Model $entry) |
||
105 | { |
||
106 | return $entry->{$this->getAttributeName()}; |
||
107 | } |
||
108 | |||
109 | protected function getEntryOriginalValue(Model $entry) |
||
110 | { |
||
111 | return $entry->getOriginal($this->getAttributeName()); |
||
112 | } |
||
113 | |||
114 | protected function shouldUploadFiles($entryValue): bool |
||
115 | { |
||
116 | return true; |
||
117 | } |
||
118 | |||
119 | protected function shouldKeepPreviousValueUnchanged(Model $entry, $entryValue): bool |
||
120 | { |
||
121 | return $entry->exists && ($entryValue === null || $entryValue === [null]); |
||
122 | } |
||
123 | |||
124 | protected function hasDeletedFiles($entryValue): bool |
||
127 | } |
||
128 | |||
129 | protected function processRepeatableUploads(Model $entry, Collection $values): Collection |
||
130 | { |
||
131 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
132 | $uploadedValues = $uploader->uploadRepeatableFiles($values->pluck($uploader->getAttributeName())->toArray(), $this->getPreviousRepeatableValues($entry, $uploader)); |
||
133 | |||
134 | $values = $values->map(function ($item, $key) use ($uploadedValues, $uploader) { |
||
135 | $item[$uploader->getAttributeName()] = $uploadedValues[$key] ?? null; |
||
136 | |||
137 | return $item; |
||
138 | }); |
||
139 | } |
||
140 | |||
141 | return $values; |
||
142 | } |
||
143 | |||
144 | private function retrieveRepeatableFiles(Model $entry): Model |
||
145 | { |
||
146 | if ($this->isRelationship) { |
||
147 | return $this->retrieveRepeatableRelationFiles($entry); |
||
148 | } |
||
149 | |||
150 | $repeatableUploaders = app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()); |
||
151 | |||
152 | $values = $entry->{$this->getRepeatableContainerName()}; |
||
153 | $values = is_string($values) ? json_decode($values, true) : $values; |
||
154 | $values = array_map(function ($item) use ($repeatableUploaders) { |
||
155 | foreach ($repeatableUploaders as $upload) { |
||
156 | $item[$upload->getAttributeName()] = $this->getValuesWithPathStripped($item, $upload); |
||
157 | } |
||
158 | |||
159 | return $item; |
||
160 | }, $values); |
||
161 | |||
162 | $entry->{$this->getRepeatableContainerName()} = $values; |
||
163 | |||
164 | return $entry; |
||
165 | } |
||
166 | |||
167 | private function retrieveRepeatableRelationFiles(Model $entry) |
||
168 | { |
||
169 | switch($this->getRepeatableRelationType()) { |
||
170 | case 'BelongsToMany': |
||
171 | case 'MorphToMany': |
||
172 | $pivotClass = app('crud')->getModel()->{$this->getUploaderSubfield()['baseEntity']}()->getPivotClass(); |
||
173 | $pivotFieldName = 'pivot_'.$this->getAttributeName(); |
||
174 | $connectedEntry = new $pivotClass([$this->getAttributeName() => $entry->$pivotFieldName]); |
||
175 | $entry->{$pivotFieldName} = $this->retrieveFiles($connectedEntry)->{$this->getAttributeName()}; |
||
176 | |||
177 | break; |
||
178 | default: |
||
179 | $entry = $this->retrieveFiles($entry); |
||
180 | } |
||
181 | |||
182 | return $entry; |
||
183 | } |
||
184 | |||
185 | private function getRepeatableRelationType() |
||
186 | { |
||
187 | return $this->getUploaderField()->getAttributes()['relation_type']; |
||
188 | } |
||
189 | |||
190 | private function getUploaderField() |
||
191 | { |
||
192 | return app('crud')->field($this->getRepeatableContainerName()); |
||
193 | } |
||
194 | |||
195 | private function getUploaderSubfield() |
||
198 | } |
||
199 | |||
200 | private function getUploaderFieldSubfields() |
||
201 | { |
||
202 | return $this->getUploaderField()->getAttributes()['subfields']; |
||
203 | } |
||
204 | |||
205 | private function deleteRepeatableFiles(Model $entry): void |
||
206 | { |
||
207 | if ($this->isRelationship) { |
||
208 | $this->deleteRelationshipFiles($entry); |
||
209 | |||
210 | return; |
||
211 | } |
||
212 | |||
213 | $repeatableValues = collect($entry->{$this->getName()}); |
||
214 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) { |
||
215 | if (! $upload->shouldDeleteFiles()) { |
||
216 | continue; |
||
217 | } |
||
218 | $values = $repeatableValues->pluck($upload->getName())->toArray(); |
||
219 | foreach ($values as $value) { |
||
220 | if (! $value) { |
||
221 | continue; |
||
222 | } |
||
223 | |||
224 | if (is_array($value)) { |
||
225 | foreach ($value as $subvalue) { |
||
226 | Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue); |
||
227 | } |
||
228 | |||
229 | continue; |
||
230 | } |
||
231 | |||
232 | Storage::disk($upload->getDisk())->delete($upload->getPath().$value); |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | /******************************* |
||
237 | * Helper methods |
||
238 | *******************************/ |
||
239 | |||
240 | /** |
||
241 | * Given two multidimensional arrays/collections, merge them recursively. |
||
242 | */ |
||
243 | protected function mergeValuesRecursive(array|Collection $array1, array|Collection $array2): array|Collection |
||
244 | { |
||
245 | $merged = $array1; |
||
246 | foreach ($array2 as $key => &$value) { |
||
247 | if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
||
248 | $merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
||
249 | } else { |
||
250 | $merged[$key] = $value; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | return $merged; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Repeatable items send `_order_` parameter in the request. |
||
259 | * This holds the order of the items in the repeatable container. |
||
260 | */ |
||
261 | protected function getFileOrderFromRequest(): array |
||
271 | } |
||
272 | |||
273 | private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array |
||
274 | { |
||
275 | $previousValues = json_decode($entry->getOriginal($uploader->getRepeatableContainerName()), true); |
||
276 | |||
277 | if (! empty($previousValues)) { |
||
278 | $previousValues = array_column($previousValues, $uploader->getName()); |
||
279 | } |
||
280 | |||
281 | return $previousValues ?? []; |
||
282 | } |
||
283 | |||
284 | private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload) |
||
285 | { |
||
286 | $uploadedValues = $item[$upload->getName()] ?? null; |
||
287 | if (is_array($uploadedValues)) { |
||
288 | return array_map(function ($value) use ($upload) { |
||
289 | return Str::after($value, $upload->getPath()); |
||
290 | }, $uploadedValues); |
||
291 | } |
||
292 | |||
293 | return isset($uploadedValues) ? Str::after($uploadedValues, $upload->getPath()) : null; |
||
294 | } |
||
295 | |||
296 | private function deleteRelationshipFiles(Model $entry): void |
||
297 | { |
||
298 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
299 | $uploader->deleteRepeatableRelationFiles($entry); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | private function deleteRepeatableRelationFiles(Model $entry) |
||
349 | } |
||
350 | } |
||
351 |