CronjobIterator.php 965 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Service\ServiceIterator;
  3. use App\Service\Cron\CronjobInterface;
  4. use RuntimeException;
  5. /**
  6. * Permet d'itérer sur les cronjobs
  7. */
  8. class CronjobIterator
  9. {
  10. /**
  11. * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'
  12. * @param iterable<CronjobInterface> $cronjobServices
  13. */
  14. public function __construct(
  15. readonly private iterable $cronjobServices,
  16. ) {}
  17. public function getByName(string $name): CronjobInterface
  18. {
  19. /** @var CronjobInterface $cronService */
  20. foreach ($this->cronjobServices as $cronService){
  21. if ($cronService->name() === $name) {
  22. return $cronService;
  23. }
  24. }
  25. throw new RuntimeException('no cronjob service found with this name : ' . $name);
  26. }
  27. /**
  28. * @return iterable<CronjobInterface>
  29. */
  30. public function getAll(): iterable {
  31. return $this->cronjobServices;
  32. }
  33. }