EncoderIterator.php 1020 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\ServiceIterator;
  4. use App\ApiResources\Export\ExportRequest;
  5. use App\Service\Export\Encoder\EncoderInterface;
  6. use Exception;
  7. /**
  8. * Permet d'itérer sur les services d'encodage
  9. */
  10. class EncoderIterator
  11. {
  12. /**
  13. * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'
  14. * @param iterable $encoders
  15. */
  16. public function __construct(
  17. private iterable $encoders,
  18. ) {}
  19. /**
  20. * Itère sur les services d'encodage disponibles et
  21. * retourne le premier qui supporte ce type de requête.
  22. *
  23. * @param string $format
  24. * @return EncoderInterface
  25. */
  26. public function getEncoderFor(string $format): EncoderInterface
  27. {
  28. /** @var EncoderInterface $encoder */
  29. foreach ($this->encoders as $encoder){
  30. if($encoder->support($format))
  31. return $encoder;
  32. }
  33. throw new \RuntimeException('no encoder found for this export request');
  34. }
  35. }