We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 48 |
| Total Lines | 249 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 7 | Features | 1 |
Complex classes like Uploader 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 Uploader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class Uploader implements UploaderInterface |
||
| 14 | { |
||
| 15 | use HandleFileNaming; |
||
| 16 | use HandleRepeatableUploads; |
||
| 17 | |||
| 18 | private string $name; |
||
| 19 | |||
| 20 | private string $disk = 'public'; |
||
| 21 | |||
| 22 | private string $path = ''; |
||
| 23 | |||
| 24 | private bool $handleMultipleFiles = false; |
||
| 25 | |||
| 26 | private bool $deleteWhenEntryIsDeleted = true; |
||
| 27 | |||
| 28 | private bool|string $attachedToFakeField = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Cloud disks have the ability to generate temporary URLs to files, should we do it? |
||
| 32 | */ |
||
| 33 | private bool $useTemporaryUrl = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * When using temporary urls, define the time that the url will be valid. |
||
| 37 | */ |
||
| 38 | private int $temporaryUrlExpirationTimeInMinutes = 1; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Indicates if the upload is relative to a relationship field/column. |
||
| 42 | */ |
||
| 43 | private bool $isRelationship = false; |
||
| 44 | |||
| 45 | public function __construct(array $crudObject, array $configuration) |
||
| 46 | { |
||
| 47 | $this->name = $crudObject['name']; |
||
| 48 | $this->disk = $configuration['disk'] ?? $crudObject['disk'] ?? $this->disk; |
||
| 49 | $this->path = $this->getPathFromConfiguration($crudObject, $configuration); |
||
| 50 | $this->attachedToFakeField = isset($crudObject['fake']) && $crudObject['fake'] ? ($crudObject['store_in'] ?? 'extras') : ($crudObject['store_in'] ?? false); |
||
| 51 | $this->useTemporaryUrl = $configuration['temporaryUrl'] ?? $this->useTemporaryUrl; |
||
| 52 | $this->temporaryUrlExpirationTimeInMinutes = $configuration['temporaryUrlExpirationTime'] ?? $this->temporaryUrlExpirationTimeInMinutes; |
||
| 53 | $this->deleteWhenEntryIsDeleted = $configuration['deleteWhenEntryIsDeleted'] ?? $this->deleteWhenEntryIsDeleted; |
||
| 54 | $this->fileNamer = is_callable($configuration['fileNamer'] ?? null) ? $configuration['fileNamer'] : $this->getFileNameGeneratorInstance($configuration['fileNamer'] ?? null); |
||
| 55 | } |
||
| 56 | |||
| 57 | /******************************* |
||
| 58 | * Static methods |
||
| 59 | *******************************/ |
||
| 60 | public static function for(array $crudObject, array $definition): UploaderInterface |
||
| 61 | { |
||
| 62 | return new static($crudObject, $definition); |
||
| 63 | } |
||
| 64 | |||
| 65 | /******************************* |
||
| 66 | * public methods - event handler methods |
||
| 67 | *******************************/ |
||
| 68 | public function storeUploadedFiles(Model $entry): Model |
||
| 69 | { |
||
| 70 | if ($this->handleRepeatableFiles) { |
||
| 71 | return $this->handleRepeatableFiles($entry); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($this->attachedToFakeField) { |
||
| 75 | $fakeFieldValue = $entry->{$this->attachedToFakeField}; |
||
| 76 | $fakeFieldValue = is_string($fakeFieldValue) ? json_decode($fakeFieldValue, true) : (array) $fakeFieldValue; |
||
| 77 | $fakeFieldValue[$this->getName()] = $this->uploadFiles($entry); |
||
|
|
|||
| 78 | |||
| 79 | $entry->{$this->attachedToFakeField} = isset($entry->getCasts()[$this->attachedToFakeField]) ? $fakeFieldValue : json_encode($fakeFieldValue); |
||
| 80 | |||
| 81 | return $entry; |
||
| 82 | } |
||
| 83 | |||
| 84 | $entry->{$this->getName()} = $this->uploadFiles($entry); |
||
| 85 | |||
| 86 | return $entry; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function retrieveUploadedFiles(Model $entry): Model |
||
| 90 | { |
||
| 91 | if ($this->handleRepeatableFiles) { |
||
| 92 | return $this->retrieveRepeatableFiles($entry); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $this->retrieveFiles($entry); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function deleteUploadedFiles(Model $entry): void |
||
| 99 | { |
||
| 100 | if ($this->deleteWhenEntryIsDeleted) { |
||
| 101 | if (! in_array(SoftDeletes::class, class_uses_recursive($entry), true)) { |
||
| 102 | $this->performFileDeletion($entry); |
||
| 103 | |||
| 104 | return; |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($entry->isForceDeleting() === true) { |
||
| 108 | $this->performFileDeletion($entry); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /******************************* |
||
| 114 | * Getters |
||
| 115 | *******************************/ |
||
| 116 | public function getName(): string |
||
| 117 | { |
||
| 118 | return $this->name; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function getDisk(): string |
||
| 122 | { |
||
| 123 | return $this->disk; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function getPath(): string |
||
| 127 | { |
||
| 128 | return $this->path; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function useTemporaryUrl(): bool |
||
| 132 | { |
||
| 133 | return $this->useTemporaryUrl; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getExpirationTimeInMinutes(): int |
||
| 137 | { |
||
| 138 | return $this->temporaryUrlExpirationTimeInMinutes; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function shouldDeleteFiles(): bool |
||
| 142 | { |
||
| 143 | return $this->deleteWhenEntryIsDeleted; |
||
| 144 | } |
||
| 145 | |||
| 146 | public function getIdentifier(): string |
||
| 147 | { |
||
| 148 | if ($this->handleRepeatableFiles) { |
||
| 149 | return $this->repeatableContainerName.'_'.$this->name; |
||
| 150 | } |
||
| 151 | |||
| 152 | return $this->name; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function canHandleMultipleFiles(): bool |
||
| 156 | { |
||
| 157 | return $this->handleMultipleFiles; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function getPreviousFiles(Model $entry): mixed |
||
| 161 | { |
||
| 162 | if (! $this->attachedToFakeField) { |
||
| 163 | return $entry->getOriginal($this->getName()); |
||
| 164 | } |
||
| 165 | |||
| 166 | $value = $entry->getOriginal($this->attachedToFakeField); |
||
| 167 | $value = is_string($value) ? json_decode($value, true) : (array) $value; |
||
| 168 | |||
| 169 | return $value[$this->getName()] ?? null; |
||
| 170 | } |
||
| 171 | |||
| 172 | /******************************* |
||
| 173 | * Setters - fluently configure the uploader |
||
| 174 | *******************************/ |
||
| 175 | public function multiple(): self |
||
| 176 | { |
||
| 177 | $this->handleMultipleFiles = true; |
||
| 178 | |||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function relationship(bool $isRelationship): self |
||
| 183 | { |
||
| 184 | $this->isRelationship = $isRelationship; |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | /******************************* |
||
| 190 | * Default implementation functions |
||
| 191 | *******************************/ |
||
| 192 | public function uploadFiles(Model $entry, $values = null) |
||
| 194 | } |
||
| 195 | |||
| 196 | private function retrieveFiles(Model $entry): Model |
||
| 197 | { |
||
| 198 | $value = $entry->{$this->name}; |
||
| 199 | |||
| 200 | if ($this->handleMultipleFiles) { |
||
| 201 | if (! isset($entry->getCasts()[$this->name]) && is_string($value)) { |
||
| 202 | $entry->{$this->name} = json_decode($value, true); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $entry; |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($this->attachedToFakeField) { |
||
| 209 | $values = $entry->{$this->attachedToFakeField}; |
||
| 210 | $values = is_string($values) ? json_decode($values, true) : (array) $values; |
||
| 211 | |||
| 212 | $values[$this->name] = isset($values[$this->name]) ? Str::after($values[$this->name], $this->path) : null; |
||
| 213 | $entry->{$this->attachedToFakeField} = json_encode($values); |
||
| 214 | |||
| 215 | return $entry; |
||
| 216 | } |
||
| 217 | |||
| 218 | $entry->{$this->name} = Str::after($value, $this->path); |
||
| 219 | |||
| 220 | return $entry; |
||
| 221 | } |
||
| 222 | |||
| 223 | private function deleteFiles(Model $entry) |
||
| 224 | { |
||
| 225 | $values = $entry->{$this->name}; |
||
| 226 | |||
| 227 | if ($this->handleMultipleFiles) { |
||
| 228 | // ensure we have an array of values when field is not casted in model. |
||
| 229 | if (! isset($entry->getCasts()[$this->name]) && is_string($values)) { |
||
| 230 | $values = json_decode($values, true); |
||
| 231 | } |
||
| 232 | foreach ($values as $value) { |
||
| 233 | Storage::disk($this->disk)->delete($this->path.$value); |
||
| 234 | } |
||
| 235 | |||
| 236 | return; |
||
| 237 | } |
||
| 238 | |||
| 239 | $values = Str::after($values, $this->path); |
||
| 240 | Storage::disk($this->disk)->delete($this->path.$values); |
||
| 241 | } |
||
| 242 | |||
| 243 | private function performFileDeletion(Model $entry) |
||
| 252 | } |
||
| 253 | |||
| 254 | /******************************* |
||
| 255 | * Private helper methods |
||
| 256 | *******************************/ |
||
| 257 | private function getPathFromConfiguration(array $crudObject, array $configuration): string |
||
| 262 | } |
||
| 263 | } |
||
| 264 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.