| Total Complexity | 52 |
| Total Lines | 255 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Asset 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 Asset, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Asset extends Model implements HasMedia |
||
| 11 | { |
||
| 12 | use InteractsWithMedia; |
||
|
|
|||
| 13 | |||
| 14 | /** |
||
| 15 | * The asset model always has one collection type. Different collection types for |
||
| 16 | * the owning model are set on the asset model instead of the media model. |
||
| 17 | */ |
||
| 18 | const MEDIA_COLLECTION = 'default'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Proxy for the data values on the associated pivot. This is the context data |
||
| 22 | * relevant and unique for each owner - asset relation. |
||
| 23 | */ |
||
| 24 | public function hasData(string $key): bool |
||
| 25 | { |
||
| 26 | if (!$this->pivot) return false; |
||
| 27 | |||
| 28 | return $this->pivot->hasData($key); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Proxy for the data values on the associated pivot. This is the context data |
||
| 33 | * relevant and unique for each owner - asset relation. |
||
| 34 | */ |
||
| 35 | public function getData(string $key, $default = null) |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Return path of the media file. In case the passed conversion |
||
| 44 | * does not exist, the path to the original is returned. |
||
| 45 | */ |
||
| 46 | public function getPath($conversionName = ''): ?string |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return url of the media file. In case the passed conversion |
||
| 53 | * does not exist, the url to the original is returned. |
||
| 54 | */ |
||
| 55 | public function getUrl(string $conversionName = '', ?string $format = null): ?string |
||
| 56 | { |
||
| 57 | if ($conversionName !== '' && $format) { |
||
| 58 | $conversionName = $format . '-' . $conversionName; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!$media = $this->getFirstMedia(self::MEDIA_COLLECTION)) { |
||
| 62 | return $this->getFallbackMediaUrl(self::MEDIA_COLLECTION) ?: null; |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($conversionName !== '') { |
||
| 66 | if ($media->hasGeneratedConversion($conversionName)) { |
||
| 67 | return $media->getUrl($conversionName) ?: null; |
||
| 68 | } |
||
| 69 | |||
| 70 | if (str_contains($conversionName, '-')) { |
||
| 71 | $conversionNameWithoutFormat = substr($conversionName, strpos($conversionName, '-') + 1); |
||
| 72 | |||
| 73 | if ($media->hasGeneratedConversion($conversionNameWithoutFormat)) { |
||
| 74 | return $media->getUrl($conversionNameWithoutFormat) ?: null; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | return $media->getUrl() ?: null; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return filename of the media file. In case the passed conversion |
||
| 84 | * does not exist, the name to the original is returned. |
||
| 85 | */ |
||
| 86 | public function getFileName(string $conversionName = ''): ?string |
||
| 87 | { |
||
| 88 | if (!$path = $this->getFirstMediaPath(self::MEDIA_COLLECTION, $conversionName)) return null; |
||
| 89 | |||
| 90 | return basename($path); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getBaseName(string $conversionName = ''): string |
||
| 94 | { |
||
| 95 | return basename($this->getFileName($conversionName), '.' . $this->getExtension()); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Checks if the conversion exists. It checks if file |
||
| 100 | * exists as media record and on the server |
||
| 101 | */ |
||
| 102 | public function exists(string $conversionName = ''): bool |
||
| 103 | { |
||
| 104 | // In case there is no media model attached to our Asset. |
||
| 105 | if (!$path = $this->getFirstMediaPath(self::MEDIA_COLLECTION, $conversionName)) { |
||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | // When we specifically check if a conversion exists, we need to explicitly check if the provided path is that of the conversion. |
||
| 110 | // This is because Media Library falls back to returning the original path if the converted file does not exist. |
||
| 111 | if ($conversionName) { |
||
| 112 | $originalPath = $this->getFirstMediaPath(self::MEDIA_COLLECTION, ''); |
||
| 113 | if ($originalPath == $path) return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | return file_exists($path); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getSize(): int |
||
| 120 | { |
||
| 121 | return $this->getMediaPropertyValue('size', 0); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function getHumanReadableSize(): string |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getMimeType(): ?string |
||
| 130 | { |
||
| 131 | return $this->getMediaPropertyValue('mime_type'); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getExtension(): string |
||
| 135 | { |
||
| 136 | return $this->getMediaPropertyValue('extension', ''); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getExtensionType(): string |
||
| 147 | }; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function isImage(): bool |
||
| 151 | { |
||
| 152 | return $this->getExtensionType() == 'image'; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getImageWidth(string $conversionName = ''): ?int |
||
| 156 | { |
||
| 157 | return $this->getImageDimensions($conversionName)['width']; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function getImageHeight(string $conversionName = ''): ?int |
||
| 161 | { |
||
| 162 | return $this->getImageDimensions($conversionName)['height']; |
||
| 163 | } |
||
| 164 | |||
| 165 | private function getImageDimensions(string $conversionName = ''): array |
||
| 182 | } |
||
| 183 | |||
| 184 | private function getMediaPropertyValue(string $property, $default = null) |
||
| 185 | { |
||
| 186 | if (!$mediaModel = $this->getFirstMedia(self::MEDIA_COLLECTION)) { |
||
| 187 | return $default; |
||
| 188 | } |
||
| 189 | |||
| 190 | return $mediaModel->{$property}; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @deprecated Use getFileName instead |
||
| 195 | */ |
||
| 196 | public function filename($size = ''): string |
||
| 197 | { |
||
| 198 | return $this->getFileName($size); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @deprecated use getUrl() instead |
||
| 203 | */ |
||
| 204 | public function url($size = ''): string |
||
| 205 | { |
||
| 206 | return $this->getUrl($size); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @deprecated use exists() instead |
||
| 211 | */ |
||
| 212 | public function hasFile(): bool |
||
| 215 | } |
||
| 216 | |||
| 217 | public function getConversions(): array |
||
| 218 | { |
||
| 219 | if (!$media = $this->getFirstMedia()) return []; |
||
| 220 | |||
| 221 | return $media |
||
| 222 | ->getGeneratedConversions() |
||
| 223 | ->keys() |
||
| 224 | ->all(); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Register the conversions that should be performed. |
||
| 229 | * |
||
| 230 | * @param Media|null $media |
||
| 231 | * @throws \Spatie\Image\Exceptions\InvalidManipulation |
||
| 232 | */ |
||
| 233 | public function registerMediaConversions(Media $media = null): void |
||
| 269 |