We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 65 |
Total Lines | 326 |
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) |
||
44 | } |
||
45 | |||
46 | protected function handleRepeatableFiles(Model $entry): Model |
||
57 | } |
||
58 | |||
59 | public static function collecFilesAndValuesFromRequest(string $attribute): array|Collection |
||
60 | { |
||
61 | $values = collect(CRUD::getRequest()->get($attribute)); |
||
62 | $files = collect(CRUD::getRequest()->file($attribute)); |
||
63 | |||
64 | return self::mergeFilesAndValuesRecursive($values, $files); |
||
65 | } |
||
66 | |||
67 | private function processRelationshipRepeatableUploaders(Model $entry) |
||
68 | { |
||
69 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
70 | $entry = $uploader->uploadRelationshipFiles($entry); |
||
71 | } |
||
72 | |||
73 | return $entry; |
||
74 | } |
||
75 | |||
76 | protected function uploadRelationshipFiles(Model $entry): Model |
||
104 | } |
||
105 | |||
106 | protected function getFilesFromEntry(Model $entry) |
||
107 | { |
||
108 | return $entry->getAttribute($this->getAttributeName()); |
||
109 | } |
||
110 | |||
111 | protected function getEntryAttributeValue(Model $entry) |
||
112 | { |
||
113 | return $entry->{$this->getAttributeName()}; |
||
114 | } |
||
115 | |||
116 | protected function getEntryOriginalValue(Model $entry) |
||
117 | { |
||
118 | return $entry->getOriginal($this->getAttributeName()); |
||
119 | } |
||
120 | |||
121 | protected function shouldUploadFiles($entryValue): bool |
||
122 | { |
||
123 | return true; |
||
124 | } |
||
125 | |||
126 | protected function shouldKeepPreviousValueUnchanged(Model $entry, $entryValue): bool |
||
127 | { |
||
128 | return $entry->exists && ($entryValue === null || $entryValue === [null]); |
||
129 | } |
||
130 | |||
131 | protected function hasDeletedFiles($entryValue): bool |
||
134 | } |
||
135 | |||
136 | protected function processRepeatableUploads(Model $entry, Collection $values): Collection |
||
137 | { |
||
138 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
139 | $uploadedValues = $uploader->uploadRepeatableFiles($values->pluck($uploader->getAttributeName())->toArray(), $this->getPreviousRepeatableValues($entry, $uploader)); |
||
140 | |||
141 | $values = $values->map(function ($item, $key) use ($uploadedValues, $uploader) { |
||
142 | $item[$uploader->getAttributeName()] = $uploadedValues[$key] ?? null; |
||
143 | |||
144 | return $item; |
||
145 | }); |
||
146 | } |
||
147 | |||
148 | return $values; |
||
149 | } |
||
150 | |||
151 | private function retrieveRepeatableFiles(Model $entry): Model |
||
152 | { |
||
153 | if ($this->isRelationship) { |
||
154 | return $this->retrieveRepeatableRelationFiles($entry); |
||
155 | } |
||
156 | |||
157 | $repeatableUploaders = app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()); |
||
158 | |||
159 | $values = $entry->{$this->getRepeatableContainerName()}; |
||
160 | $values = is_string($values) ? json_decode($values, true) : $values; |
||
161 | $values = array_map(function ($item) use ($repeatableUploaders) { |
||
162 | foreach ($repeatableUploaders as $upload) { |
||
163 | $item[$upload->getAttributeName()] = $this->getValuesWithPathStripped($item, $upload); |
||
164 | } |
||
165 | |||
166 | return $item; |
||
167 | }, $values); |
||
168 | |||
169 | $entry->{$this->getRepeatableContainerName()} = $values; |
||
170 | |||
171 | return $entry; |
||
172 | } |
||
173 | |||
174 | private function retrieveRepeatableRelationFiles(Model $entry) |
||
175 | { |
||
176 | switch($this->getRepeatableRelationType()) { |
||
177 | case 'BelongsToMany': |
||
178 | case 'MorphToMany': |
||
179 | $pivotClass = app('crud')->getModel()->{$this->getUploaderSubfield()['baseEntity']}()->getPivotClass(); |
||
180 | $pivotFieldName = 'pivot_'.$this->getAttributeName(); |
||
181 | $connectedEntry = new $pivotClass([$this->getAttributeName() => $entry->$pivotFieldName]); |
||
182 | $entry->{$pivotFieldName} = $this->retrieveFiles($connectedEntry)->{$this->getAttributeName()}; |
||
183 | |||
184 | break; |
||
185 | default: |
||
186 | $entry = $this->retrieveFiles($entry); |
||
187 | } |
||
188 | |||
189 | return $entry; |
||
190 | } |
||
191 | |||
192 | private function getRepeatableRelationType() |
||
193 | { |
||
194 | return $this->getUploaderField()->getAttributes()['relation_type']; |
||
195 | } |
||
196 | |||
197 | private function getUploaderField() |
||
198 | { |
||
199 | return app('crud')->field($this->getRepeatableContainerName()); |
||
200 | } |
||
201 | |||
202 | private function getUploaderSubfield() |
||
205 | } |
||
206 | |||
207 | private function getUploaderFieldSubfields() |
||
208 | { |
||
209 | return $this->getUploaderField()->getAttributes()['subfields']; |
||
210 | } |
||
211 | |||
212 | private function deleteRepeatableFiles(Model $entry): void |
||
213 | { |
||
214 | if ($this->isRelationship) { |
||
215 | $this->deleteRelationshipFiles($entry); |
||
216 | |||
217 | return; |
||
218 | } |
||
219 | |||
220 | $repeatableValues = collect($entry->{$this->getName()}); |
||
221 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) { |
||
222 | if (! $upload->shouldDeleteFiles()) { |
||
223 | continue; |
||
224 | } |
||
225 | $values = $repeatableValues->pluck($upload->getName())->toArray(); |
||
226 | foreach ($values as $value) { |
||
227 | if (! $value) { |
||
228 | continue; |
||
229 | } |
||
230 | |||
231 | if (is_array($value)) { |
||
232 | foreach ($value as $subvalue) { |
||
233 | Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue); |
||
234 | } |
||
235 | |||
236 | continue; |
||
237 | } |
||
238 | |||
239 | Storage::disk($upload->getDisk())->delete($upload->getPath().$value); |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /******************************* |
||
245 | * Helper methods |
||
246 | *******************************/ |
||
247 | /** |
||
248 | * Repeatable items send `_order_` parameter in the request. |
||
249 | * This holds the order of the items in the repeatable container. |
||
250 | */ |
||
251 | protected function getFileOrderFromRequest(): array |
||
261 | } |
||
262 | |||
263 | private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array |
||
264 | { |
||
265 | $previousValues = json_decode($entry->getOriginal($uploader->getRepeatableContainerName()), true); |
||
266 | |||
267 | if (! empty($previousValues)) { |
||
268 | $previousValues = array_column($previousValues, $uploader->getName()); |
||
269 | } |
||
270 | |||
271 | return $previousValues ?? []; |
||
272 | } |
||
273 | |||
274 | private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload) |
||
275 | { |
||
276 | $uploadedValues = $item[$upload->getName()] ?? null; |
||
277 | if (is_array($uploadedValues)) { |
||
278 | return array_map(function ($value) use ($upload) { |
||
279 | return Str::after($value, $upload->getPath()); |
||
280 | }, $uploadedValues); |
||
281 | } |
||
282 | |||
283 | return isset($uploadedValues) ? Str::after($uploadedValues, $upload->getPath()) : null; |
||
284 | } |
||
285 | |||
286 | private function deleteRelationshipFiles(Model $entry): void |
||
287 | { |
||
288 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
||
289 | $uploader->deleteRepeatableRelationFiles($entry); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | private function deleteRepeatableRelationFiles(Model $entry) |
||
339 | } |
||
340 | } |
||
341 |