We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 43 |
| Total Lines | 230 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 6 | 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 |
||
| 12 | trait HandleRepeatableUploads |
||
| 13 | { |
||
| 14 | public bool $handleRepeatableFiles = false; |
||
| 15 | |||
| 16 | public ?string $repeatableContainerName = null; |
||
| 17 | |||
| 18 | /******************************* |
||
| 19 | * Setters - fluently configure the uploader |
||
| 20 | *******************************/ |
||
| 21 | public function repeats(string $repeatableContainerName): self |
||
| 22 | { |
||
| 23 | $this->handleRepeatableFiles = true; |
||
| 24 | |||
| 25 | $this->repeatableContainerName = $repeatableContainerName; |
||
| 26 | |||
| 27 | return $this; |
||
| 28 | } |
||
| 29 | |||
| 30 | /******************************* |
||
| 31 | * Getters |
||
| 32 | *******************************/ |
||
| 33 | public function getRepeatableContainerName(): ?string |
||
| 34 | { |
||
| 35 | return $this->repeatableContainerName; |
||
| 36 | } |
||
| 37 | |||
| 38 | /******************************* |
||
| 39 | * Default implementation methods |
||
| 40 | *******************************/ |
||
| 41 | protected function uploadRepeatableFiles($values, $previousValues, $entry = null) |
||
| 42 | { |
||
| 43 | } |
||
| 44 | |||
| 45 | protected function handleRepeatableFiles(Model $entry): Model |
||
| 46 | { |
||
| 47 | $values = collect(CRUD::getRequest()->get($this->getRepeatableContainerName())); |
||
|
|
|||
| 48 | $files = collect(CRUD::getRequest()->file($this->getRepeatableContainerName())); |
||
| 49 | $value = $this->mergeValuesRecursive($values, $files); |
||
| 50 | |||
| 51 | if ($this->isRelationship) { |
||
| 52 | return $this->uploadRelationshipFiles($entry, $value); |
||
| 53 | } |
||
| 54 | |||
| 55 | $entry->{$this->getRepeatableContainerName()} = json_encode($this->processRepeatableUploads($entry, $value)); |
||
| 56 | |||
| 57 | return $entry; |
||
| 58 | } |
||
| 59 | |||
| 60 | private function uploadRelationshipFiles(Model $entry, mixed $value): Model |
||
| 72 | } |
||
| 73 | |||
| 74 | protected function processRepeatableUploads(Model $entry, Collection $values): Collection |
||
| 87 | } |
||
| 88 | |||
| 89 | private function retrieveRepeatableFiles(Model $entry): Model |
||
| 90 | { |
||
| 91 | if ($this->isRelationship) { |
||
| 92 | return $this->retrieveRepeatableRelationFiles($entry); |
||
| 93 | } |
||
| 94 | |||
| 95 | $repeatableUploaders = app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()); |
||
| 96 | |||
| 97 | $values = $entry->{$this->getRepeatableContainerName()}; |
||
| 98 | $values = is_string($values) ? json_decode($values, true) : $values; |
||
| 99 | $values = array_map(function ($item) use ($repeatableUploaders) { |
||
| 100 | foreach ($repeatableUploaders as $upload) { |
||
| 101 | $item[$upload->getName()] = $this->getValuesWithPathStripped($item, $upload); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $item; |
||
| 105 | }, $values); |
||
| 106 | |||
| 107 | $entry->{$this->getRepeatableContainerName()} = $values; |
||
| 108 | |||
| 109 | return $entry; |
||
| 110 | } |
||
| 111 | |||
| 112 | private function retrieveRepeatableRelationFiles(Model $entry) |
||
| 113 | { |
||
| 114 | switch($this->getRepeatableRelationType()) { |
||
| 115 | case 'BelongsToMany': |
||
| 116 | $pivotClass = app('crud')->getModel()->{$this->getUploaderSubfield()['baseEntity']}()->getPivotClass(); |
||
| 117 | $pivotFieldName = 'pivot_'.$this->getName(); |
||
| 118 | $connectedEntry = new $pivotClass([$this->getName() => $entry->$pivotFieldName]); |
||
| 119 | $entry->{$pivotFieldName} = $this->retrieveFiles($connectedEntry)->{$this->getName()}; |
||
| 120 | |||
| 121 | break; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $entry; |
||
| 125 | } |
||
| 126 | |||
| 127 | private function getRepeatableRelationType() |
||
| 128 | { |
||
| 129 | return $this->getUploaderField()->getAttributes()['relation_type']; |
||
| 130 | } |
||
| 131 | |||
| 132 | private function getUploaderField() |
||
| 133 | { |
||
| 134 | return app('crud')->field($this->getRepeatableContainerName()); |
||
| 135 | } |
||
| 136 | |||
| 137 | private function getUploaderSubfield() |
||
| 140 | } |
||
| 141 | |||
| 142 | private function getUploaderFieldSubfields() |
||
| 143 | { |
||
| 144 | return $this->getUploaderField()->getAttributes()['subfields']; |
||
| 145 | } |
||
| 146 | |||
| 147 | private function deleteRepeatableFiles(Model $entry): void |
||
| 148 | { |
||
| 149 | if ($this->isRelationship) { |
||
| 150 | switch($this->getRepeatableRelationType()) { |
||
| 151 | case 'BelongsToMany': |
||
| 152 | // handle belongs to many deletion |
||
| 153 | return; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->deleteFiles($entry); |
||
| 157 | |||
| 158 | return; |
||
| 159 | } |
||
| 160 | |||
| 161 | $repeatableValues = collect($entry->{$this->getName()}); |
||
| 162 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) { |
||
| 163 | if (! $upload->shouldDeleteFiles()) { |
||
| 164 | continue; |
||
| 165 | } |
||
| 166 | $values = $repeatableValues->pluck($upload->getName())->toArray(); |
||
| 167 | foreach ($values as $value) { |
||
| 168 | if (! $value) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (is_array($value)) { |
||
| 173 | foreach ($value as $subvalue) { |
||
| 174 | Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue); |
||
| 175 | } |
||
| 176 | |||
| 177 | continue; |
||
| 178 | } |
||
| 179 | |||
| 180 | Storage::disk($upload->getDisk())->delete($upload->getPath().$value); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | /******************************* |
||
| 185 | * Helper methods |
||
| 186 | *******************************/ |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Given two multidimensional arrays/collections, merge them recursively. |
||
| 190 | */ |
||
| 191 | protected function mergeValuesRecursive(array|Collection $array1, array|Collection $array2): array|Collection |
||
| 192 | { |
||
| 193 | $merged = $array1; |
||
| 194 | foreach ($array2 as $key => &$value) { |
||
| 195 | if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
||
| 196 | $merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
||
| 197 | } else { |
||
| 198 | $merged[$key] = $value; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | return $merged; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Repeatable items send `_order_` parameter in the request. |
||
| 207 | * This holds the order of the items in the repeatable container. |
||
| 208 | */ |
||
| 209 | protected function getFileOrderFromRequest(): array |
||
| 219 | } |
||
| 220 | |||
| 221 | private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array |
||
| 222 | { |
||
| 223 | $previousValues = json_decode($entry->getOriginal($uploader->getRepeatableContainerName()), true); |
||
| 224 | |||
| 225 | if (! empty($previousValues)) { |
||
| 226 | $previousValues = array_column($previousValues, $uploader->getName()); |
||
| 227 | } |
||
| 228 | |||
| 229 | return $previousValues ?? []; |
||
| 230 | } |
||
| 231 | |||
| 232 | private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload) |
||
| 242 | } |
||
| 243 | } |
||
| 244 |