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