We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 47 |
| Total Lines | 262 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| 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 | { |
||
| 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 | default: |
||
| 123 | $entry = $this->retrieveFiles($entry); |
||
| 124 | } |
||
| 125 | |||
| 126 | return $entry; |
||
| 127 | } |
||
| 128 | |||
| 129 | private function getRepeatableRelationType() |
||
| 130 | { |
||
| 131 | return $this->getUploaderField()->getAttributes()['relation_type']; |
||
| 132 | } |
||
| 133 | |||
| 134 | private function getUploaderField() |
||
| 135 | { |
||
| 136 | return app('crud')->field($this->getRepeatableContainerName()); |
||
| 137 | } |
||
| 138 | |||
| 139 | private function getUploaderSubfield() |
||
| 142 | } |
||
| 143 | |||
| 144 | private function getUploaderFieldSubfields() |
||
| 145 | { |
||
| 146 | return $this->getUploaderField()->getAttributes()['subfields']; |
||
| 147 | } |
||
| 148 | |||
| 149 | private function deleteRepeatableFiles(Model $entry): void |
||
| 150 | { |
||
| 151 | if ($this->isRelationship) { |
||
| 152 | switch($this->getRepeatableRelationType()) { |
||
| 153 | case 'BelongsToMany': |
||
| 154 | $pivotAttributes = $entry->getAttributes(); |
||
| 155 | $connectedPivot = $entry->pivotParent->{$this->getRepeatableContainerName()}->where(function($item) use ($pivotAttributes) { |
||
| 156 | |||
| 157 | $itemPivotAttributes = $item->pivot->only(array_keys($pivotAttributes)); |
||
| 158 | |||
| 159 | return $itemPivotAttributes === $pivotAttributes; |
||
| 160 | |||
| 161 | })->first(); |
||
| 162 | |||
| 163 | if(!$connectedPivot) { |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | $files = $connectedPivot->getOriginal()['pivot_'.$this->getName()]; |
||
| 168 | |||
| 169 | if(!$files) { |
||
| 170 | return; |
||
| 171 | } |
||
| 172 | |||
| 173 | if (is_array($files)) { |
||
| 174 | foreach ($files as $value) { |
||
| 175 | $value = Str::start($value, $this->getPath()); |
||
| 176 | Storage::disk($this->getDisk())->delete($value); |
||
| 177 | } |
||
| 178 | |||
| 179 | return; |
||
| 180 | } |
||
| 181 | |||
| 182 | $value = Str::start($files, $this->getPath()); |
||
| 183 | Storage::disk($this->getDisk())->delete($value); |
||
| 184 | |||
| 185 | return; |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->deleteFiles($entry); |
||
| 189 | |||
| 190 | return; |
||
| 191 | } |
||
| 192 | |||
| 193 | $repeatableValues = collect($entry->{$this->getName()}); |
||
| 194 | foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) { |
||
| 195 | if (! $upload->shouldDeleteFiles()) { |
||
| 196 | continue; |
||
| 197 | } |
||
| 198 | $values = $repeatableValues->pluck($upload->getName())->toArray(); |
||
| 199 | foreach ($values as $value) { |
||
| 200 | if (! $value) { |
||
| 201 | continue; |
||
| 202 | } |
||
| 203 | |||
| 204 | if (is_array($value)) { |
||
| 205 | foreach ($value as $subvalue) { |
||
| 206 | Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue); |
||
| 207 | } |
||
| 208 | |||
| 209 | continue; |
||
| 210 | } |
||
| 211 | |||
| 212 | Storage::disk($upload->getDisk())->delete($upload->getPath().$value); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | /******************************* |
||
| 217 | * Helper methods |
||
| 218 | *******************************/ |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Given two multidimensional arrays/collections, merge them recursively. |
||
| 222 | */ |
||
| 223 | protected function mergeValuesRecursive(array|Collection $array1, array|Collection $array2): array|Collection |
||
| 224 | { |
||
| 225 | $merged = $array1; |
||
| 226 | foreach ($array2 as $key => &$value) { |
||
| 227 | if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
||
| 228 | $merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
||
| 229 | } else { |
||
| 230 | $merged[$key] = $value; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | return $merged; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Repeatable items send `_order_` parameter in the request. |
||
| 239 | * This holds the order of the items in the repeatable container. |
||
| 240 | */ |
||
| 241 | protected function getFileOrderFromRequest(): array |
||
| 251 | } |
||
| 252 | |||
| 253 | private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array |
||
| 254 | { |
||
| 255 | $previousValues = json_decode($entry->getOriginal($uploader->getRepeatableContainerName()), true); |
||
| 256 | |||
| 257 | if (! empty($previousValues)) { |
||
| 258 | $previousValues = array_column($previousValues, $uploader->getName()); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $previousValues ?? []; |
||
| 262 | } |
||
| 263 | |||
| 264 | private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload) |
||
| 274 | } |
||
| 275 | } |
||
| 276 |