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