<?php

namespace App\Service\ServiceIterator;

use App\Service\Cron\CronjobInterface;
use RuntimeException;

/**
 * Permet d'itérer sur les cronjobs
 */
class CronjobIterator
{
    /**
     * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'
     * @param iterable<CronjobInterface> $cronjobServices
     */
    public function __construct(
        readonly private iterable $cronjobServices,
    ) {}

    public function getByName(string $name): CronjobInterface
    {
        /** @var CronjobInterface $cronService */
        foreach ($this->cronjobServices as $cronService){
            if ($cronService->name() === $name) {
                return $cronService;
            }
        }
        throw new RuntimeException('no cronjob service found with this name : ' . $name);
    }

    /**
     * @return iterable<CronjobInterface>
     */
    public function getAll(): iterable {
        return $this->cronjobServices;
    }
}
