ldc-file-storage-service/src/Lib/PathGenerators/UuidPathGenerator.php

30 lines
885 B
PHP

<?php
declare(strict_types=1);
namespace Uwil\Storage\Lib\PathGenerators;
use Illuminate\Support\Str;
use Uwil\Storage\Contracts\PathGenerator;
use Uwil\Storage\Lib\ValueObjects\DiskName;
use Uwil\Storage\Lib\ValueObjects\SanitizedFile;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
class UuidPathGenerator implements PathGenerator
{
#[\Override]
public function generate(DiskName $disk, SanitizedFile $file, ?string $directory = null): StorageFilePath
{
$extension = pathinfo($file->sanitizedName, PATHINFO_EXTENSION);
$uuid = Str::uuid()->toString();
$finalFilename = $uuid . (! empty($extension) ? '.' . $extension : '');
$pathSegments = array_filter([
$directory ? trim($directory, '/') : null,
$finalFilename,
]);
return new StorageFilePath(implode('/', $pathSegments));
}
}