StorageIterator.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\ServiceIterator;
  4. use App\Entity\Core\File;
  5. use App\Service\File\Storage\FileStorageInterface;
  6. /**
  7. * Permet d'itérer sur les services d'export.
  8. */
  9. class StorageIterator
  10. {
  11. /**
  12. * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'.
  13. *
  14. * @param iterable<FileStorageInterface> $storageServices
  15. */
  16. public function __construct(
  17. readonly private iterable $storageServices,
  18. ) {
  19. }
  20. public function getStorages(): iterable
  21. {
  22. return $this->storageServices;
  23. }
  24. /**
  25. * Itère sur les services de storage disponibles et
  26. * retourne le premier qui supporte ce type de requête.
  27. *
  28. * @throws \Exception
  29. */
  30. public function getStorageFor(File $file): FileStorageInterface
  31. {
  32. /** @var FileStorageInterface $storageService */
  33. foreach ($this->storageServices as $storageService) {
  34. if ($storageService->support($file)) {
  35. return $storageService;
  36. }
  37. }
  38. throw new \RuntimeException('no storage service found for this File');
  39. }
  40. }