103 lines
2.9 KiB
PHP
103 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\Artwork;
|
|
|
|
final class ArtworkFeaturedImagePath
|
|
{
|
|
/**
|
|
* @return array<string, array<string, int|string>>
|
|
*/
|
|
public function variants(): array
|
|
{
|
|
/** @var array<string, array<string, int|string>> $variants */
|
|
$variants = config('uploads.featured_variants', []);
|
|
|
|
return $variants;
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
public function variantNames(): array
|
|
{
|
|
return array_keys($this->variants());
|
|
}
|
|
|
|
public function defaultVariant(): string
|
|
{
|
|
return 'desktop';
|
|
}
|
|
|
|
public function featuredPrefix(): string
|
|
{
|
|
return trim((string) config('uploads.featured_prefix', 'artworks/featured'), '/');
|
|
}
|
|
|
|
public function objectPath(Artwork $artwork, string $variant): string
|
|
{
|
|
$hash = $this->normalizedHash($artwork);
|
|
$variantName = $this->normalizeVariant($variant);
|
|
|
|
return sprintf(
|
|
'%s/%s/%s/%s/%s.webp',
|
|
$this->featuredPrefix(),
|
|
$variantName,
|
|
substr($hash, 0, 2),
|
|
substr($hash, 2, 2),
|
|
$hash,
|
|
);
|
|
}
|
|
|
|
public function url(Artwork $artwork, string $variant): string
|
|
{
|
|
return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/').'/'.$this->objectPath($artwork, $variant);
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
public function preferredVariantOrder(string $variant): array
|
|
{
|
|
$variantName = $this->normalizeVariant($variant);
|
|
|
|
$orders = [
|
|
'mobile_xs' => ['mobile_xs', 'mobile_sm', 'mobile', 'tablet', 'desktop', 'desktop_xl'],
|
|
'mobile_sm' => ['mobile_sm', 'mobile_xs', 'mobile', 'tablet', 'desktop', 'desktop_xl'],
|
|
'mobile' => ['mobile', 'mobile_sm', 'mobile_xs', 'tablet', 'desktop', 'desktop_xl'],
|
|
'tablet' => ['tablet', 'desktop', 'desktop_xl', 'mobile', 'mobile_sm', 'mobile_xs'],
|
|
'desktop' => ['desktop', 'desktop_xl', 'tablet', 'mobile', 'mobile_sm', 'mobile_xs'],
|
|
'desktop_xl' => ['desktop_xl', 'desktop', 'tablet', 'mobile', 'mobile_sm', 'mobile_xs'],
|
|
];
|
|
|
|
return $orders[$variantName] ?? [$this->defaultVariant()];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, int|string>|null
|
|
*/
|
|
public function variantConfig(string $variant): ?array
|
|
{
|
|
return $this->variants()[$this->normalizeVariant($variant)] ?? null;
|
|
}
|
|
|
|
public function normalizeVariant(string $variant): string
|
|
{
|
|
return array_key_exists($variant, $this->variants()) ? $variant : $this->defaultVariant();
|
|
}
|
|
|
|
private function normalizedHash(Artwork $artwork): string
|
|
{
|
|
$hash = trim((string) $artwork->hash);
|
|
|
|
if ($hash === '') {
|
|
throw new \InvalidArgumentException('Artwork hash is required for featured thumbnail paths.');
|
|
}
|
|
|
|
return $hash;
|
|
}
|
|
}
|