<?php

declare(strict_types=1);

namespace apexl\Io\services;

use apexl\Config\ConfigurationInterface;
use apexl\Config\Exceptions\MissingConfigException;
use apexl\Io\interfaces\LazyLoadAllowerInterface;

final class LazyLoadAllowerFromConfigService implements LazyLoadAllowerInterface
{
    public ?bool $override = null;

    public function __construct(private readonly ConfigurationInterface $configuration) {}

    public function override(?bool $override): ?bool
    {
        $old = $this->override;
        $this->override = $override;

        return $old;
    }

    public function isAllowed(): bool
    {
        try {
            if (isset($this->override)) {
                return $this->override;
            }

            return $this->configuration->get('entity.lazy_load');
        } catch (MissingConfigException) {
            return false;
        }
    }
}