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