Complex classes like HasMedia 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 HasMedia, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | trait HasMedia |
||
| 27 | { |
||
| 28 | /** @var Conversion[] */ |
||
| 29 | public $mediaConversions = []; |
||
| 30 | |||
| 31 | /** @var MediaCollection[] */ |
||
| 32 | public $mediaCollections = []; |
||
| 33 | |||
| 34 | /** @var bool */ |
||
| 35 | protected $deletePreservingMedia = false; |
||
| 36 | |||
| 37 | /** @var array */ |
||
| 38 | protected $unAttachedMediaLibraryItems = []; |
||
| 39 | |||
| 40 | public static function bootHasMediaTrait() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Set the polymorphic relation. |
||
| 59 | * |
||
| 60 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 61 | */ |
||
| 62 | public function media() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Add a file to the medialibrary. |
||
| 69 | * |
||
| 70 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 71 | * |
||
| 72 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 73 | */ |
||
| 74 | public function addMedia($file) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Add a file from the given disk. |
||
| 81 | * |
||
| 82 | * @param string $key |
||
| 83 | * @param string $disk |
||
| 84 | * |
||
| 85 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 86 | */ |
||
| 87 | public function addMediaFromDisk(string $key, string $disk = null) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Add a file from a request. |
||
| 94 | * |
||
| 95 | * @param string $key |
||
| 96 | * |
||
| 97 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 98 | */ |
||
| 99 | public function addMediaFromRequest(string $key) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Add multiple files from a request by keys. |
||
| 106 | * |
||
| 107 | * @param string[] $keys |
||
| 108 | * |
||
| 109 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 110 | */ |
||
| 111 | public function addMultipleMediaFromRequest(array $keys) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Add all files from a request. |
||
| 118 | * |
||
| 119 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 120 | */ |
||
| 121 | public function addAllMediaFromRequest() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Add a remote file to the medialibrary. |
||
| 128 | * |
||
| 129 | * @param string $url |
||
| 130 | * @param string|array ...$allowedMimeTypes |
||
| 131 | * |
||
| 132 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 133 | * |
||
| 134 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 135 | */ |
||
| 136 | public function addMediaFromUrl(string $url, ...$allowedMimeTypes) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Add a base64 encoded file to the medialibrary. |
||
| 168 | * |
||
| 169 | * @param string $base64data |
||
| 170 | * @param string|array ...$allowedMimeTypes |
||
| 171 | * |
||
| 172 | * @throws InvalidBase64Data |
||
| 173 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 174 | * |
||
| 175 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 176 | */ |
||
| 177 | public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Copy a file to the medialibrary. |
||
| 210 | * |
||
| 211 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 212 | * |
||
| 213 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 214 | */ |
||
| 215 | public function copyMedia($file) |
||
| 219 | |||
| 220 | /* |
||
| 221 | * Determine if there is media in the given collection. |
||
| 222 | */ |
||
| 223 | public function hasMedia(string $collectionName = 'default'): bool |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get media collection by its collectionName. |
||
| 230 | * |
||
| 231 | * @param string $collectionName |
||
| 232 | * @param array|callable $filters |
||
| 233 | * |
||
| 234 | * @return \Illuminate\Support\Collection |
||
| 235 | */ |
||
| 236 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
| 240 | |||
| 241 | public function getFirstMedia(string $collectionName = 'default', array $filters = []): ?Media |
||
| 247 | |||
| 248 | /* |
||
| 249 | * Get the url of the image for the given conversionName |
||
| 250 | * for first media for the given collectionName. |
||
| 251 | * If no profile is given, return the source's url. |
||
| 252 | */ |
||
| 253 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
| 263 | |||
| 264 | /* |
||
| 265 | * Get the url of the image for the given conversionName |
||
| 266 | * for first media for the given collectionName. |
||
| 267 | * |
||
| 268 | * If no profile is given, return the source's url. |
||
| 269 | */ |
||
| 270 | public function getFirstTemporaryUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
| 280 | |||
| 281 | public function getMediaCollection(string $collectionName = 'default'): ?MediaCollection |
||
| 290 | |||
| 291 | public function getFallbackMediaUrl(string $collectionName = 'default'): string |
||
| 295 | |||
| 296 | public function getFallbackMediaPath(string $collectionName = 'default'): string |
||
| 300 | |||
| 301 | /* |
||
| 302 | * Get the url of the image for the given conversionName |
||
| 303 | * for first media for the given collectionName. |
||
| 304 | * If no profile is given, return the source's url. |
||
| 305 | */ |
||
| 306 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Update a media collection by deleting and inserting again with new values. |
||
| 319 | * |
||
| 320 | * @param array $newMediaArray |
||
| 321 | * @param string $collectionName |
||
| 322 | * |
||
| 323 | * @return \Illuminate\Support\Collection |
||
| 324 | * |
||
| 325 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 326 | */ |
||
| 327 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 360 | |||
| 361 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Remove all media in the given collection. |
||
| 372 | * |
||
| 373 | * @param string $collectionName |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function clearMediaCollection(string $collectionName = 'default'): self |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Remove all media in the given collection except some. |
||
| 393 | * |
||
| 394 | * @param string $collectionName |
||
| 395 | * @param \Spatie\MediaLibrary\Models\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Delete the associated media with the given id. |
||
| 430 | * You may also pass a media object. |
||
| 431 | * |
||
| 432 | * @param int|\Spatie\MediaLibrary\Models\Media $mediaId |
||
| 433 | * |
||
| 434 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 435 | */ |
||
| 436 | public function deleteMedia($mediaId) |
||
| 450 | |||
| 451 | /* |
||
| 452 | * Add a conversion. |
||
| 453 | */ |
||
| 454 | public function addMediaConversion(string $name): Conversion |
||
| 462 | |||
| 463 | public function addMediaCollection(string $name): MediaCollection |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Delete the model, but preserve all the associated media. |
||
| 474 | * |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function deletePreservingMedia(): bool |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 486 | * |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | public function shouldDeletePreservingMedia() |
||
| 493 | |||
| 494 | protected function mediaIsPreloaded(): bool |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Cache the media on the object. |
||
| 501 | * |
||
| 502 | * @param string $collectionName |
||
| 503 | * |
||
| 504 | * @return mixed |
||
| 505 | */ |
||
| 506 | public function loadMedia(string $collectionName) |
||
| 523 | |||
| 524 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
| 528 | |||
| 529 | public function processUnattachedMedia(callable $callable) |
||
| 537 | |||
| 538 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
| 555 | |||
| 556 | public function registerMediaConversions(Media $media = null) |
||
| 559 | |||
| 560 | public function registerMediaCollections() |
||
| 563 | |||
| 564 | public function registerAllMediaConversions(Media $media = null) |
||
| 587 | } |
||
| 588 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: