<?php


namespace AutoAdmin\Bundle\Seo\Extractor;


use AutoAdmin\Bundle\Seo\Model\SeoPage;
use Symfony\Component\HttpFoundation\Request;

class ExtractorManager
{
    private array $extractors;

    public function __construct(array $extractors = [])
    {
        $this->extractors = $extractors;
    }

    /**
     * todo: allow extractors to only handle a part of the data,
     * todo: in order to combine multiple extractors to rebuild SeoPage from different sources
     */
    public function extract(Request $request, object $subject): ?SeoPage
    {
        $extracted = null;
        $route = $request->get('_route');

        /** @var ExtractorInterface $extractor */
        foreach ($this->extractors as $extractor) {
            if ($extractor->supports($subject, $route)) {
                /** @var SeoPage $extracted */
                $extracted = $extractor->extract($request, $subject);
                break;
            }
        }

        return $extracted;
    }
}