<?php

declare(strict_types=1);

namespace App\Service\ServiceIterator\Mailer;

use App\Service\Mailer\Model\MailerModelInterface;
use App\Service\Mailer\Builder\BuilderInterface;
use Exception;

/**
 * Permet d'itérer sur les services de build Mailer
 */
class BuilderIterator
{
    /**
     * Pour l'injection des services, voir config/services.yaml, section 'TAG Services'
     * @param iterable $builderServices
     */
    public function __construct(
        private iterable $builderServices,
    ) {}

    /**
     * Itère sur les services de build disponibles et
     * retourne le premier qui supporte ce type de requête.
     *
     * @param string $type
     * @return BuilderInterface
     * @throws Exception
     */
    public function getBuilderFor(MailerModelInterface $mailerModel): BuilderInterface
    {
        /** @var BuilderInterface $builderServices */
        foreach ($this->builderServices as $builderService){
            if($builderService->support($mailerModel))
                return $builderService;
        }
        throw new Exception('no builder service found for this operation');
    }
}
