We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 70 |
| Total Lines | 511 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 | /** |
||
| 16 | * The name of the uploader AKA CrudField/Column name. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public string $name; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Indicates the uploaded file should be deleted when entry is deleted. |
||
| 24 | * |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | public $deleteWhenEntryIsDeleted = true; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Indicates if this uploader instance is inside a repeatable container. |
||
| 31 | * |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | public $isRepeatable = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * When inside a repeatable container, indicates the container name. |
||
| 38 | * |
||
| 39 | * @var string|null |
||
| 40 | */ |
||
| 41 | public $repeatableContainerName = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Developer provided filename. |
||
| 45 | * |
||
| 46 | * @var null|string|Closure |
||
| 47 | */ |
||
| 48 | public $fileName = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The disk where upload will be stored. By default `public`. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $disk = 'public'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Indicates if the upload handles multiple files. |
||
| 59 | * |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | public $isMultiple = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The path inside the disk to store the uploads. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public $path = ''; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Should the url to the object be a temporary one (eg: s3). |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | public $useTemporaryUrl = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * When using temporary urls, defines the time that the url |
||
| 80 | * should be available in minutes. |
||
| 81 | * |
||
| 82 | * By default 1 minute |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | public $temporaryUrlExpirationTime = 1; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Indicates if the upload is relative to a relationship field/column. |
||
| 90 | * |
||
| 91 | * @var bool |
||
| 92 | */ |
||
| 93 | public $isRelationship = false; |
||
| 94 | |||
| 95 | public function __construct(array $crudObject, array $configuration) |
||
| 96 | { |
||
| 97 | $this->name = $crudObject['name']; |
||
| 98 | $this->disk = $configuration['disk'] ?? $crudObject['disk'] ?? $this->disk; |
||
| 99 | $this->useTemporaryUrl = $configuration['temporary'] ?? $this->useTemporaryUrl; |
||
| 100 | $this->temporaryUrlExpirationTime = $configuration['expiration'] ?? $this->temporaryUrlExpirationTime; |
||
| 101 | $this->path = $configuration['path'] ?? $crudObject['prefix'] ?? $this->path; |
||
| 102 | $this->path = empty($this->path) ? $this->path : Str::of($this->path)->finish('/')->value; |
||
| 103 | $this->fileName = $configuration['fileName'] ?? $this->fileName; |
||
| 104 | $this->deleteWhenEntryIsDeleted = $configuration['whenDelete'] ?? $this->deleteWhenEntryIsDeleted; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * An abstract function that all uploaders must implement with a single file save process. |
||
| 109 | * |
||
| 110 | * @param Model $entry |
||
| 111 | * @param mixed $values |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | abstract public function uploadFile(Model $entry, $values = null); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * An function that all uploaders must implement if it also supports repeatable files |
||
| 118 | * |
||
| 119 | * @param Model $entry |
||
| 120 | * @param mixed $values |
||
| 121 | * @return mixed |
||
| 122 | */ |
||
| 123 | public function saveRepeatableFile(Model $entry, $values = null) |
||
| 124 | { |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getRepeatableContainerName() |
||
| 128 | { |
||
| 129 | return $this->repeatableContainerName; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * The function called in the saving event that starts the upload process. |
||
| 134 | * |
||
| 135 | * @param Model $entry |
||
| 136 | * @return Model |
||
| 137 | */ |
||
| 138 | public function processFileUpload(Model $entry) |
||
| 139 | { |
||
| 140 | if ($this->isRepeatable) { |
||
| 141 | return $this->uploadRepeatableFile($entry); |
||
| 142 | } |
||
| 143 | |||
| 144 | $entry->{$this->name} = $this->uploadFile($entry); |
||
| 145 | |||
| 146 | return $entry; |
||
| 147 | } |
||
| 148 | |||
| 149 | private function uploadRepeatableFile(Model $entry) |
||
| 150 | { |
||
| 151 | $values = collect(CRUD::getRequest()->get($this->repeatableContainerName)); |
||
|
|
|||
| 152 | $files = collect(CRUD::getRequest()->file($this->repeatableContainerName)); |
||
| 153 | |||
| 154 | $value = $this->mergeValuesRecursive($values, $files); |
||
| 155 | |||
| 156 | if (! $this->isRelationship) { |
||
| 157 | $entry->{$this->repeatableContainerName} = json_encode($this->processRepeatableUploads($entry, $value)); |
||
| 158 | } else { |
||
| 159 | $modelCount = CRUD::get('uploaded_'.$this->repeatableContainerName.'_count'); |
||
| 160 | |||
| 161 | $value = $value->slice($modelCount, 1)->toArray(); |
||
| 162 | |||
| 163 | foreach (app('UploadersRepository')->getRegisteredUploadersFor($this->repeatableContainerName) as $uploader) { |
||
| 164 | if (array_key_exists($modelCount, $value) && isset($value[$modelCount][$uploader->getName()])) { |
||
| 165 | $entry->{$uploader->getName()} = $uploader->uploadFile($entry, $value[$modelCount][$uploader->getName()]); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return $entry; |
||
| 171 | } |
||
| 172 | |||
| 173 | private function processRepeatableUploads(Model $entry, $value) |
||
| 174 | { |
||
| 175 | foreach (app('UploadersRepository')->getRegisteredUploadersFor($this->repeatableContainerName) as $uploader) { |
||
| 176 | $uploadedValues = $uploader->saveRepeatableFile($entry, $value->pluck($uploader->name)->toArray()); |
||
| 177 | |||
| 178 | $value = $value->map(function ($item, $key) use ($uploadedValues, $uploader) { |
||
| 179 | $item[$uploader->getName()] = $uploadedValues[$key] ?? null; |
||
| 180 | |||
| 181 | return $item; |
||
| 182 | }); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $value; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Return the uploader name. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getName() |
||
| 194 | { |
||
| 195 | return $this->name; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return the uploader disk. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getDisk() |
||
| 204 | { |
||
| 205 | return $this->disk; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Return the uploader path. |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function getPath() |
||
| 214 | { |
||
| 215 | return $this->path; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Return the uploader temporary option. |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function getTemporary() |
||
| 224 | { |
||
| 225 | return $this->useTemporaryUrl; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Return the uploader expiration time in minutes. |
||
| 230 | * |
||
| 231 | * @return int |
||
| 232 | */ |
||
| 233 | public function getExpiration() |
||
| 234 | { |
||
| 235 | return $this->temporaryUrlExpirationTime; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * The function called in the retrieved event that handles the display of uploaded values. |
||
| 240 | * |
||
| 241 | * @param Model $entry |
||
| 242 | * @return Model |
||
| 243 | */ |
||
| 244 | public function retrieveUploadedFile(Model $entry) |
||
| 245 | { |
||
| 246 | if ($this->isRepeatable) { |
||
| 247 | return $this->retrieveRepeatableFiles($entry); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $this->retrieveFile($entry); |
||
| 251 | } |
||
| 252 | |||
| 253 | protected function retrieveFile($entry) |
||
| 254 | { |
||
| 255 | $value = $entry->{$this->name}; |
||
| 256 | |||
| 257 | if ($this->isMultiple && ! isset($entry->getCasts()[$this->name]) && is_string($value)) { |
||
| 258 | $entry->{$this->name} = json_decode($value, true); |
||
| 259 | } else { |
||
| 260 | $entry->{$this->name} = Str::after($value, $this->path); |
||
| 261 | } |
||
| 262 | |||
| 263 | return $entry; |
||
| 264 | } |
||
| 265 | |||
| 266 | protected function retrieveRepeatableFiles($entry) |
||
| 267 | { |
||
| 268 | if ($this->isRelationship) { |
||
| 269 | return $this->retrieveFile($entry); |
||
| 270 | } |
||
| 271 | |||
| 272 | return $entry; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * The function called in the deleting event to delete the uploaded files upon entry deletion. |
||
| 277 | * |
||
| 278 | * @param Model $entry |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | public function deleteUploadedFile(Model $entry) |
||
| 282 | { |
||
| 283 | if ($this->deleteWhenEntryIsDeleted) { |
||
| 284 | if (! in_array(SoftDeletes::class, class_uses_recursive($entry), true)) { |
||
| 285 | $this->performFileDeletion($entry); |
||
| 286 | } else { |
||
| 287 | if ($entry->forceDeleting === true) { |
||
| 288 | $this->performFileDeletion($entry); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | private function deleteRepeatableFiles($entry) |
||
| 295 | { |
||
| 296 | if($this->isRelationship) { |
||
| 297 | return $this->deleteFiles($entry); |
||
| 298 | } |
||
| 299 | |||
| 300 | $repeatableValues = collect($entry->{$this->getName()}); |
||
| 301 | foreach (app('UploadersRepository')->getRegisteredUploadersFor($this->repeatableContainerName) as $upload) { |
||
| 302 | if (! $upload->deleteWhenEntryIsDeleted) { |
||
| 303 | continue; |
||
| 304 | } |
||
| 305 | $values = $repeatableValues->pluck($upload->getName())->toArray(); |
||
| 306 | foreach ($values as $value) { |
||
| 307 | if (! $value) { |
||
| 308 | continue; |
||
| 309 | } |
||
| 310 | if (is_array($value)) { |
||
| 311 | foreach ($value as $subvalue) { |
||
| 312 | Storage::disk($upload->disk)->delete($upload->path.$subvalue); |
||
| 313 | } |
||
| 314 | |||
| 315 | continue; |
||
| 316 | } |
||
| 317 | Storage::disk($upload->disk)->delete($upload->path.$value); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | private function deleteFiles($entry) |
||
| 323 | { |
||
| 324 | $values = $entry->{$this->name}; |
||
| 325 | |||
| 326 | if ($this->isMultiple) { |
||
| 327 | if (! isset($entry->getCasts()[$this->name]) && is_string($values)) { |
||
| 328 | $values = json_decode($values, true); |
||
| 329 | } |
||
| 330 | } else { |
||
| 331 | $values = (array) Str::after($values, $this->path); |
||
| 332 | } |
||
| 333 | |||
| 334 | foreach ($values as $value) { |
||
| 335 | Storage::disk($this->disk)->delete($this->path.$value); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | |||
| 340 | private function performFileDeletion($entry) |
||
| 341 | { |
||
| 342 | if( $this->isRelationship) { |
||
| 343 | return $this->deleteFiles($entry); |
||
| 344 | } |
||
| 345 | |||
| 346 | $this->deleteRepeatableFiles($entry); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Build an uploader instance. |
||
| 351 | * |
||
| 352 | * @param array $crudObject |
||
| 353 | * @param array $definition |
||
| 354 | * @return self |
||
| 355 | */ |
||
| 356 | public static function for(array $crudObject, array $definition) |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set multiple attribute to true in the uploader. |
||
| 363 | * |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | protected function multiple() |
||
| 367 | { |
||
| 368 | $this->isMultiple = true; |
||
| 369 | |||
| 370 | return $this; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set relationship attribute in uploader. |
||
| 375 | * When true, it also removes the repeatable in case the relationship is handled |
||
| 376 | * |
||
| 377 | * @param bool $isRelationship |
||
| 378 | * @return self |
||
| 379 | */ |
||
| 380 | public function relationship(bool $isRelationship): self |
||
| 381 | { |
||
| 382 | $this->isRelationship = $isRelationship; |
||
| 383 | |||
| 384 | return $this; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set the repeatable attribute to true in the uploader and the |
||
| 389 | * corresponding container name. |
||
| 390 | * |
||
| 391 | * @param string $repeatableContainerName |
||
| 392 | * @return self |
||
| 393 | */ |
||
| 394 | public function repeats(string $repeatableContainerName): self |
||
| 395 | { |
||
| 396 | $this->isRepeatable = true; |
||
| 397 | |||
| 398 | $this->repeatableContainerName = $repeatableContainerName; |
||
| 399 | |||
| 400 | return $this; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Repeatable items send _order_ parameter in the request. |
||
| 405 | * This olds the information for uploads inside repeatable containers. |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | protected function getFileOrderFromRequest() |
||
| 410 | { |
||
| 411 | $items = CRUD::getRequest()->input('_order_'.$this->repeatableContainerName) ?? []; |
||
| 412 | |||
| 413 | array_walk($items, function (&$key, $value) { |
||
| 414 | $requestValue = $key[$this->name] ?? null; |
||
| 415 | $key = $this->isMultiple ? (is_string($requestValue) ? explode(',', $requestValue) : $requestValue) : $requestValue; |
||
| 416 | }); |
||
| 417 | |||
| 418 | return $items; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Return a new instance of the entry class for the uploader. |
||
| 423 | * |
||
| 424 | * @return Model |
||
| 425 | */ |
||
| 426 | protected function modelInstance() |
||
| 427 | { |
||
| 428 | //return new $this->entryClass; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Return the uploader stored values when in a repeatable container. |
||
| 433 | * |
||
| 434 | * @param Model $entry |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | protected function getPreviousRepeatableValues(Model $entry) |
||
| 438 | { |
||
| 439 | $previousValues = json_decode($entry->getOriginal($this->repeatableContainerName), true); |
||
| 440 | if (! empty($previousValues)) { |
||
| 441 | $previousValues = array_column($previousValues, $this->name); |
||
| 442 | } |
||
| 443 | |||
| 444 | return $previousValues ?? []; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Return the file extension. |
||
| 449 | * |
||
| 450 | * @param mixed $file |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | protected function getExtensionFromFile($file) |
||
| 454 | { |
||
| 455 | return is_a($file, UploadedFile::class, true) ? $file->extension() : Str::after(mime_content_type($file), '/'); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Return the file name built by Backpack or by the developer in `fileName` configuration. |
||
| 460 | * |
||
| 461 | * @param mixed $file |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | protected function getFileName($file) |
||
| 465 | { |
||
| 466 | if (is_file($file)) { |
||
| 467 | return Str::of($this->fileNameFrom($file) ?? Str::of($file->getClientOriginalName())->beforeLast('.')->slug()->append('-'.Str::random(4))); |
||
| 468 | } |
||
| 469 | |||
| 470 | return Str::of($this->fileNameFrom($file) ?? Str::random(40)); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Return the complete filename and extension. |
||
| 475 | * |
||
| 476 | * @param mixed $file |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | protected function getFileNameWithExtension($file) |
||
| 480 | { |
||
| 481 | if (is_file($file)) { |
||
| 482 | return $this->getFileName($file).'.'.$this->getExtensionFromFile($file); |
||
| 483 | } |
||
| 484 | |||
| 485 | return Str::of($this->fileNameFrom($file) ?? Str::random(40)).'.'.$this->getExtensionFromFile($file); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Allow developer to override the default Backpack fileName. |
||
| 490 | * |
||
| 491 | * @param mixed $file |
||
| 492 | * @return string|null |
||
| 493 | */ |
||
| 494 | private function fileNameFrom($file) |
||
| 501 | } |
||
| 502 | |||
| 503 | protected function mergeValuesRecursive($array1, $array2) |
||
| 504 | { |
||
| 505 | $merged = $array1; |
||
| 506 | foreach ($array2 as $key => &$value) { |
||
| 507 | if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
||
| 508 | $merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
||
| 509 | } else { |
||
| 510 | $merged[$key] = $value; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | return $merged; |
||
| 515 | } |
||
| 516 | |||
| 517 | public function getIdentifier() |
||
| 524 | } |
||
| 525 | } |
||
| 526 |