| Total Complexity | 64 | 
| Total Lines | 435 | 
| Duplicated Lines | 0 % | 
| Changes | 5 | ||
| Bugs | 2 | Features | 0 | 
Complex classes like HasMedias 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 HasMedias, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 11 | trait HasMedias | ||
| 12 | { | ||
| 13 | protected $cropParamsKeys = [ | ||
| 14 | 'crop_x', | ||
| 15 | 'crop_y', | ||
| 16 | 'crop_w', | ||
| 17 | 'crop_h', | ||
| 18 | ]; | ||
| 19 | |||
| 20 | public static function bootHasMedias(): void | ||
| 21 |     { | ||
| 22 |         self::deleted(static function (Model $model) { | ||
| 23 |             if (!method_exists($model, 'isForceDeleting') || $model->isForceDeleting()) { | ||
| 24 | /** @var \A17\Twill\Models\Behaviors\HasMedias $model */ | ||
| 25 | $model->medias()->detach(); | ||
| 26 | } | ||
| 27 | }); | ||
| 28 | } | ||
| 29 | |||
| 30 | /** | ||
| 31 | * Defines the many-to-many relationship for media objects. | ||
| 32 | * | ||
| 33 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany | ||
| 34 | */ | ||
| 35 | public function medias() | ||
| 36 |     { | ||
| 37 | return $this->morphToMany( | ||
|  | |||
| 38 | Media::class, | ||
| 39 | 'mediable', | ||
| 40 |             config('twill.mediables_table', 'twill_mediables') | ||
| 41 | )->withPivot(array_merge([ | ||
| 42 | 'crop', | ||
| 43 | 'role', | ||
| 44 | 'crop_w', | ||
| 45 | 'crop_h', | ||
| 46 | 'crop_x', | ||
| 47 | 'crop_y', | ||
| 48 | 'lqip_data', | ||
| 49 | 'ratio', | ||
| 50 | 'metadatas', | ||
| 51 |         ], config('twill.media_library.translated_form_fields', false) ? ['locale'] : [])) | ||
| 52 |             ->withTimestamps()->orderBy(config('twill.mediables_table', 'twill_mediables') . '.id', 'asc'); | ||
| 53 | } | ||
| 54 | |||
| 55 | private function findMedia($role, $crop = "default") | ||
| 56 |     { | ||
| 57 | $foundMedia = false; | ||
| 58 |         $media = $this->medias->first(function ($media) use ($role, $crop, &$foundMedia) { | ||
| 59 |             if (config('twill.media_library.translated_form_fields', false)) { | ||
| 60 | $localeScope = $media->pivot->locale === app()->getLocale(); | ||
| 61 | } | ||
| 62 | |||
| 63 |             if (!$foundMedia) { | ||
| 64 | $foundMedia = $media->pivot->role === $role && ($localeScope ?? true); | ||
| 65 | } | ||
| 66 | |||
| 67 | return $foundMedia && $media->pivot->crop === $crop; | ||
| 68 | }); | ||
| 69 | |||
| 70 |         if (!$media && config('twill.media_library.translated_form_fields', false)) { | ||
| 71 |             $media = $this->medias->first(function ($media) use ($role, $crop, &$foundMedia) { | ||
| 72 |                 if (!$foundMedia) { | ||
| 73 | $foundMedia = $media->pivot->role === $role; | ||
| 74 | } | ||
| 75 | |||
| 76 | return $foundMedia && $media->pivot->crop === $crop; | ||
| 77 | }); | ||
| 78 | } | ||
| 79 | |||
| 80 |         if ($foundMedia && !$media && config('app.debug')) { | ||
| 81 | // In this case we found the media but not the crop because our result is still empty. | ||
| 82 | throw new MediaCropNotFoundException($crop); | ||
| 83 | } | ||
| 84 | |||
| 85 | return $media; | ||
| 86 | } | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Checks if an image has been attached for a role and crop. | ||
| 90 | * | ||
| 91 | * @param string $role Role name. | ||
| 92 | * @param string $crop Crop name. | ||
| 93 | * @return bool | ||
| 94 | */ | ||
| 95 | public function hasImage($role, $crop = "default") | ||
| 96 |     { | ||
| 97 | $media = $this->findMedia($role, $crop); | ||
| 98 | |||
| 99 | return !empty($media); | ||
| 100 | } | ||
| 101 | |||
| 102 | /** | ||
| 103 | * Returns the URL of the attached image for a role and crop. | ||
| 104 | * | ||
| 105 | * @param string $role Role name. | ||
| 106 | * @param string $crop Crop name. | ||
| 107 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 108 | * @param bool $has_fallback Indicate that you can provide a fallback. Will return `null` instead of the default image fallback. | ||
| 109 | * @param bool $cms Indicate that you are displaying this image in the CMS views. | ||
| 110 | * @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries. | ||
| 111 | * @return string|null | ||
| 112 | */ | ||
| 113 | public function image($role, $crop = "default", $params = [], $has_fallback = false, $cms = false, $media = null) | ||
| 114 |     { | ||
| 115 | |||
| 116 |         if (!$media) { | ||
| 117 | $media = $this->findMedia($role, $crop); | ||
| 118 | } | ||
| 119 | |||
| 120 |         if ($media) { | ||
| 121 | |||
| 122 | $crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys); | ||
| 123 | |||
| 124 |             if ($cms) { | ||
| 125 | |||
| 126 | return ImageService::getCmsUrl($media->uuid, $crop_params + $params); | ||
| 127 | } | ||
| 128 | |||
| 129 | return ImageService::getUrlWithCrop($media->uuid, $crop_params, $params); | ||
| 130 | } | ||
| 131 | |||
| 132 |         if ($has_fallback) { | ||
| 133 | return null; | ||
| 134 | } | ||
| 135 | |||
| 136 | return ImageService::getTransparentFallbackUrl(); | ||
| 137 | } | ||
| 138 | |||
| 139 | /** | ||
| 140 | * Returns an array of URLs of all attached images for a role and crop. | ||
| 141 | * | ||
| 142 | * @param string $role Role name. | ||
| 143 | * @param string $crop Crop name. | ||
| 144 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 145 | * @return array | ||
| 146 | */ | ||
| 147 | public function images($role, $crop = "default", $params = []) | ||
| 148 |     { | ||
| 149 |         $medias = $this->medias->filter(function ($media) use ($role, $crop) { | ||
| 150 | return $media->pivot->role === $role && $media->pivot->crop === $crop; | ||
| 151 | }); | ||
| 152 | |||
| 153 | $urls = []; | ||
| 154 | |||
| 155 |         foreach ($medias as $media) { | ||
| 156 | $urls[] = $this->image($role, $crop, $params, false, false, $media); | ||
| 157 | } | ||
| 158 | |||
| 159 | return $urls; | ||
| 160 | } | ||
| 161 | |||
| 162 | /** | ||
| 163 | * Returns an array of URLs of all attached images for a role, including all crops. | ||
| 164 | * | ||
| 165 | * @param string $role Role name. | ||
| 166 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 167 | * @return array | ||
| 168 | */ | ||
| 169 | public function imagesWithCrops($role, $params = []) | ||
| 170 |     { | ||
| 171 |         $medias = $this->medias->filter(function ($media) use ($role) { | ||
| 172 | return $media->pivot->role === $role; | ||
| 173 | }); | ||
| 174 | |||
| 175 | $urls = []; | ||
| 176 | |||
| 177 |         foreach ($medias as $media) { | ||
| 178 | $paramsForCrop = $params[$media->pivot->crop] ?? []; | ||
| 179 | $urls[$media->id][$media->pivot->crop] = $this->image($role, $media->pivot->crop, $paramsForCrop, false, false, $media); | ||
| 180 | } | ||
| 181 | |||
| 182 | return $urls; | ||
| 183 | } | ||
| 184 | |||
| 185 | /** | ||
| 186 | * Returns an array of meta information for the image attached for a role and crop. | ||
| 187 | * | ||
| 188 | * @param string $role Role name. | ||
| 189 | * @param string $crop Crop name. | ||
| 190 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 191 | * @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries. | ||
| 192 | * @return array | ||
| 193 | */ | ||
| 194 | public function imageAsArray($role, $crop = "default", $params = [], $media = null) | ||
| 195 |     { | ||
| 196 |         if (!$media) { | ||
| 197 | $media = $this->findMedia($role, $crop); | ||
| 198 | } | ||
| 199 | |||
| 200 |         if ($media) { | ||
| 201 | return [ | ||
| 202 | 'src' => $this->image($role, $crop, $params, false, false, $media), | ||
| 203 | 'width' => $media->pivot->crop_w ?? $media->width, | ||
| 204 | 'height' => $media->pivot->crop_h ?? $media->height, | ||
| 205 | 'alt' => $this->imageAltText($role, $media), | ||
| 206 | 'caption' => $this->imageCaption($role, $media), | ||
| 207 | 'video' => $this->imageVideo($role, $media), | ||
| 208 | ]; | ||
| 209 | } | ||
| 210 | |||
| 211 | return []; | ||
| 212 | } | ||
| 213 | |||
| 214 | /** | ||
| 215 | * Returns an array of meta information for all images attached for a role and crop. | ||
| 216 | * | ||
| 217 | * @param string $role Role name. | ||
| 218 | * @param string $crop Crop name. | ||
| 219 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 220 | * @return array | ||
| 221 | */ | ||
| 222 | public function imagesAsArrays($role, $crop = "default", $params = []) | ||
| 223 |     { | ||
| 224 |         $medias = $this->medias->filter(function ($media) use ($role, $crop) { | ||
| 225 | return $media->pivot->role === $role && $media->pivot->crop === $crop; | ||
| 226 | }); | ||
| 227 | |||
| 228 | $arrays = []; | ||
| 229 | |||
| 230 |         foreach ($medias as $media) { | ||
| 231 | $arrays[] = $this->imageAsArray($role, $crop, $params, $media); | ||
| 232 | } | ||
| 233 | |||
| 234 | return $arrays; | ||
| 235 | } | ||
| 236 | |||
| 237 | /** | ||
| 238 | * Returns an array of meta information for all images attached for a role, including all crops. | ||
| 239 | * | ||
| 240 | * @param string $role Role name. | ||
| 241 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 242 | * @return array | ||
| 243 | */ | ||
| 244 | public function imagesAsArraysWithCrops($role, $params = []) | ||
| 245 |     { | ||
| 246 |         $medias = $this->medias->filter(function ($media) use ($role) { | ||
| 247 | return $media->pivot->role === $role; | ||
| 248 | }); | ||
| 249 | |||
| 250 | $arrays = []; | ||
| 251 | |||
| 252 |         foreach ($medias as $media) { | ||
| 253 | $paramsForCrop = $params[$media->pivot->crop] ?? []; | ||
| 254 | $arrays[$media->id][$media->pivot->crop] = $this->imageAsArray($role, $media->pivot->crop, $paramsForCrop, $media); | ||
| 255 | } | ||
| 256 | |||
| 257 | return $arrays; | ||
| 258 | } | ||
| 259 | |||
| 260 | /** | ||
| 261 | * Returns the alt text of the image attached for a role. | ||
| 262 | * | ||
| 263 | * @param string $role Role name. | ||
| 264 | * @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries. | ||
| 265 | * @return string | ||
| 266 | */ | ||
| 267 | public function imageAltText($role, $media = null) | ||
| 268 |     { | ||
| 269 |         if (!$media) { | ||
| 270 |             $media = $this->medias->first(function ($media) use ($role) { | ||
| 271 |                 if (config('twill.media_library.translated_form_fields', false)) { | ||
| 272 | $localeScope = $media->pivot->locale === app()->getLocale(); | ||
| 273 | } | ||
| 274 | |||
| 275 | return $media->pivot->role === $role && ($localeScope ?? true);; | ||
| 276 | }); | ||
| 277 | } | ||
| 278 | |||
| 279 |         if ($media) { | ||
| 280 |             return $media->getMetadata('altText', 'alt_text'); | ||
| 281 | } | ||
| 282 | |||
| 283 | return ''; | ||
| 284 | } | ||
| 285 | |||
| 286 | /** | ||
| 287 | * Returns the caption of the image attached for a role. | ||
| 288 | * | ||
| 289 | * @param string $role Role name. | ||
| 290 | * @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries. | ||
| 291 | * @return string | ||
| 292 | */ | ||
| 293 | public function imageCaption($role, $media = null) | ||
| 294 |     { | ||
| 295 |         if (!$media) { | ||
| 296 |             $media = $this->medias->first(function ($media) use ($role) { | ||
| 297 |                 if (config('twill.media_library.translated_form_fields', false)) { | ||
| 298 | $localeScope = $media->pivot->locale === app()->getLocale(); | ||
| 299 | } | ||
| 300 | |||
| 301 | return $media->pivot->role === $role && ($localeScope ?? true);; | ||
| 302 | }); | ||
| 303 | } | ||
| 304 | |||
| 305 |         if ($media) { | ||
| 306 |             return $media->getMetadata('caption'); | ||
| 307 | } | ||
| 308 | |||
| 309 | return ''; | ||
| 310 | } | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Returns the video URL of the image attached for a role. | ||
| 314 | * | ||
| 315 | * @param string $role Role name. | ||
| 316 | * @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries. | ||
| 317 | * @return string | ||
| 318 | */ | ||
| 319 | public function imageVideo($role, $media = null) | ||
| 320 |     { | ||
| 321 |         if (!$media) { | ||
| 322 |             $media = $this->medias->first(function ($media) use ($role) { | ||
| 323 |                 if (config('twill.media_library.translated_form_fields', false)) { | ||
| 324 | $localeScope = $media->pivot->locale === app()->getLocale(); | ||
| 325 | } | ||
| 326 | |||
| 327 | return $media->pivot->role === $role && ($localeScope ?? true);; | ||
| 328 | }); | ||
| 329 | } | ||
| 330 | |||
| 331 |         if ($media) { | ||
| 332 | $metadatas = (object) json_decode($media->pivot->metadatas); | ||
| 333 | $language = app()->getLocale(); | ||
| 334 | return $metadatas->video->$language ?? (is_object($metadatas->video) ? '' : ($metadatas->video ?? '')); | ||
| 335 | } | ||
| 336 | |||
| 337 | return ''; | ||
| 338 | } | ||
| 339 | |||
| 340 | /** | ||
| 341 | * Returns the media object attached for a role and crop. | ||
| 342 | * | ||
| 343 | * @param string $role Role name. | ||
| 344 | * @param string $crop Crop name. | ||
| 345 | * @return Media|null | ||
| 346 | */ | ||
| 347 | public function imageObject($role, $crop = "default") | ||
| 348 |     { | ||
| 349 | return $this->findMedia($role, $crop); | ||
| 350 | } | ||
| 351 | |||
| 352 | /** | ||
| 353 | * Returns the LQIP base64 encoded string for a role. | ||
| 354 | * Use this in conjunction with the `RefreshLQIP` Artisan command. | ||
| 355 | * | ||
| 356 | * @param string $role Role name. | ||
| 357 | * @param string $crop Crop name. | ||
| 358 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 359 | * @param bool $has_fallback Indicate that you can provide a fallback. Will return `null` instead of the default image fallback. | ||
| 360 | * @return string|null | ||
| 361 | * @see \A17\Twill\Commands\RefreshLQIP | ||
| 362 | */ | ||
| 363 | public function lowQualityImagePlaceholder($role, $crop = "default", $params = [], $has_fallback = false) | ||
| 364 |     { | ||
| 365 | $media = $this->findMedia($role, $crop); | ||
| 366 | |||
| 367 |         if ($media) { | ||
| 368 | return $media->pivot->lqip_data ?? ImageService::getTransparentFallbackUrl(); | ||
| 369 | } | ||
| 370 | |||
| 371 |         if ($has_fallback) { | ||
| 372 | return null; | ||
| 373 | } | ||
| 374 | |||
| 375 | return ImageService::getTransparentFallbackUrl(); | ||
| 376 | |||
| 377 | } | ||
| 378 | |||
| 379 | /** | ||
| 380 | * Returns the URL of the social image for a role and crop. | ||
| 381 | * | ||
| 382 | * @param string $role Role name. | ||
| 383 | * @param string $crop Crop name. | ||
| 384 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 385 | * @param bool $has_fallback Indicate that you can provide a fallback. Will return `null` instead of the default image fallback. | ||
| 386 | * @return string|null | ||
| 387 | */ | ||
| 388 | public function socialImage($role, $crop = "default", $params = [], $has_fallback = false) | ||
| 389 |     { | ||
| 390 | $media = $this->findMedia($role, $crop); | ||
| 391 | |||
| 392 |         if ($media) { | ||
| 393 | $crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys); | ||
| 394 | |||
| 395 | return ImageService::getSocialUrl($media->uuid, $crop_params + $params); | ||
| 396 | } | ||
| 397 | |||
| 398 |         if ($has_fallback) { | ||
| 399 | return null; | ||
| 400 | } | ||
| 401 | |||
| 402 | return ImageService::getSocialFallbackUrl(); | ||
| 403 | } | ||
| 404 | |||
| 405 | /** | ||
| 406 | * Returns the URL of the CMS image for a role and crop. | ||
| 407 | * | ||
| 408 | * @param string $role Role name. | ||
| 409 | * @param string $crop Crop name. | ||
| 410 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 411 | * @return string | ||
| 412 | */ | ||
| 413 | public function cmsImage($role, $crop = "default", $params = []) | ||
| 414 |     { | ||
| 415 | return $this->image($role, $crop, $params, false, true, false) ?? ImageService::getTransparentFallbackUrl($params); | ||
| 416 | } | ||
| 417 | |||
| 418 | /** | ||
| 419 | * Returns the URL of the default CMS image for this model. | ||
| 420 | * | ||
| 421 | * @param array $params Parameters compatible with the current image service, like `w` or `h`. | ||
| 422 | * @return string | ||
| 423 | */ | ||
| 424 | public function defaultCmsImage($params = []) | ||
| 433 | } | ||
| 434 | |||
| 435 | /** | ||
| 436 | * Returns the media objects associated with a role and crop. | ||
| 437 | * | ||
| 438 | * @param string $role Role name. | ||
| 439 | * @param string $crop Crop name. | ||
| 440 | * @return \Illuminate\Database\Eloquent\Collection | ||
| 441 | */ | ||
| 442 | public function imageObjects($role, $crop = "default") | ||
| 446 | }); | ||
| 447 | } | ||
| 448 | } | ||
| 449 |