| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- declare(strict_types=1);
- namespace App\Service\ServiceIterator;
- use App\Entity\Core\File;
- use App\Service\File\Storage\FileStorageInterface;
- /**
- * Permet d'itérer sur les services d'export.
- */
- class StorageIterator
- {
- /**
- * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'.
- *
- * @param iterable<FileStorageInterface> $storageServices
- */
- public function __construct(
- readonly private iterable $storageServices,
- ) {
- }
- public function getStorages(): iterable
- {
- return $this->storageServices;
- }
- /**
- * Itère sur les services de storage disponibles et
- * retourne le premier qui supporte ce type de requête.
- *
- * @throws \Exception
- */
- public function getStorageFor(File $file): FileStorageInterface
- {
- /** @var FileStorageInterface $storageService */
- foreach ($this->storageServices as $storageService) {
- if ($storageService->support($file)) {
- return $storageService;
- }
- }
- throw new \RuntimeException('no storage service found for this File');
- }
- }
|