Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
66.67% |
4 / 6 |
|
60.00% |
3 / 5 |
|
60.00% |
3 / 5 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DefaultFileStorageService | |
66.67% |
4 / 6 |
|
60.00% |
3 / 5 |
|
60.00% |
3 / 5 |
|
60.00% |
3 / 5 |
6.60 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findProvider | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| store | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Shared\Infrastructure\Service; |
| 4 | |
| 5 | use App\Shared\Domain\Exception\FileStorageException; |
| 6 | use App\Shared\Domain\Model\StoredFile; |
| 7 | use App\Shared\Domain\Model\TempFile; |
| 8 | use App\Shared\Domain\Port\Driven\FileStorageService; |
| 9 | use Symfony\Component\DependencyInjection\Attribute\AutowireIterator; |
| 10 | |
| 11 | /** |
| 12 | * @author Wilhelm Zwertvaegher |
| 13 | */ |
| 14 | readonly class DefaultFileStorageService implements FileStorageService |
| 15 | { |
| 16 | |
| 17 | /** |
| 18 | * @var array<FileStorageProvider> |
| 19 | */ |
| 20 | private array $providers; |
| 21 | |
| 22 | /** |
| 23 | * @param iterable<FileStorageProvider> $providers |
| 24 | */ |
| 25 | public function __construct( |
| 26 | #[AutowireIterator('app.file_storage_provider')] |
| 27 | iterable $providers |
| 28 | ) { |
| 29 | $this->providers = iterator_to_array($providers); |
| 30 | } |
| 31 | |
| 32 | private function findProvider(string $type): FileStorageProvider |
| 33 | { |
| 34 | return array_find($this->providers, fn (FileStorageProvider $provider) => $provider->supports($type)) ?? |
| 35 | throw new FileStorageException("No provider found for {$type}"); |
| 36 | } |
| 37 | |
| 38 | public function store(TempFile $tempFile, string $type): TempFile |
| 39 | { |
| 40 | return $this->findProvider($type)->store($tempFile, $type); |
| 41 | } |
| 42 | |
| 43 | public function delete(StoredFile $storedFile): void |
| 44 | { |
| 45 | $this->findProvider($storedFile->getType())->delete($storedFile); |
| 46 | } |
| 47 | |
| 48 | public function generateUrl(StoredFile $storedFile): string |
| 49 | { |
| 50 | return $this->findProvider($storedFile->getType())->generateUrl($storedFile); |
| 51 | } |
| 52 | } |