ldc-ovoteam/app/Lib/PublicS3Disk.php
Jérémy Munsch 19a2af8d5b
refactor(PublicS3Disk): improve publicUrlOf method logic
Refactor the publicUrlOf method to enhance readability and maintainability.
2026-11-05 12:19:07 +01:00

49 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Lib;
use Illuminate\Filesystem\AwsS3V3Adapter;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Uwil\Storage\Lib\ValueObjects\DiskName;
final readonly class PublicS3Disk
{
public const DISK_NAME = 's3-public';
public DiskName $disk;
public string $urlEndpoint;
private AwsS3V3Adapter $adapter;
public function __construct(
private readonly string $s3PublicEndpoint,
) {
$this->disk = new DiskName(self::DISK_NAME, 's3');
$this->urlEndpoint = $this->s3PublicEndpoint;
$adapter = Storage::disk($this->disk->value);
if (! ($adapter instanceof AwsS3V3Adapter)) {
throw new \UnexpectedValueException("Expected {$this->disk->value} to be a S3 disk");
}
$this->adapter = $adapter;
}
public function publicUrlOf(string $path): string
{
$prefix = trim(Config::string('storage-service.prefix', 'media'), '/');
$trimmedPath = ltrim($path, '/');
$fullPath = when(
str_starts_with($trimmedPath, $prefix . '/'),
$trimmedPath,
($prefix !== '' ? "{$prefix}/{$trimmedPath}" : $trimmedPath)
);
return $this->adapter->publicUrl($fullPath);
}
}