<?php

namespace AutoAdmin\Bundle\Content\Service;

use AutoAdmin\Bundle\Content\Logger\LoggerInterface;
use Traversable;

class LoggerService
{
    /**
     * @var array<LoggerInterface>
     */
    protected array $loggers;

    public function __construct(Traversable $guessers)
    {
        $this->loggers = array_map([$this, 'addRouter'], iterator_to_array($guessers));
    }

    public function isLoggable($entity)
    {
        $loggable = true;
        foreach ($this->loggers as $logger) {
            if ($logger->support($entity)) {
                $loggable = $logger->isLoggable();
                break;
            }
        }

        return $loggable;
    }

    protected function addRouter(LoggerInterface $logger): LoggerInterface
    {
        return $logger;
    }
}
