Lines 100.00% 35 / 35
Methods 100.00% 4 / 4
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromSection 100.00% 22 / 22 100.00% 1 / 1 2
 requireHomeAssistant 100.00% 1 / 1 100.00% 1 / 1 1
 readConnectionConfig 100.00% 11 / 11 100.00% 1 / 1 1
18final readonly class StewartConfig
19{
20    public function __construct(
21        public int $workers,
22        public LogLevel $logLevel,
23        public LogFormat $logFormat,
24        public Duration $shutdownGrace,
25        public int $workerEventBuffer,
26        public int $workerStateBatch,
27        public int $subscriptionBuffer,
28        public ?ConnectionConfig $homeAssistant,
29        public BackoffPolicy $reconnectBackoff,
30        public SupervisionConfig $supervision,
31        public ServiceCallPolicy $serviceCalls,
32        public PersistenceConfig $persistence,
33        public ?MqttConfig $mqtt,
34        public ControlConfig $control,
35        public HttpConfig $http,
36        public CodegenConfig $codegen,
37        public GitDeployConfig $gitDeploy,
38        public ExposeConfig $expose,
39        public AppOverrideCollection $apps,
40    ) {}
41
42    /**
43     * @throws ConfigurationException
44     * @throws IdentifierException
45     */
46    public static function fromSection(#[SensitiveParameter] ConfigSection $config): self
47    {
48        $homeAssistant = $config->findSection('home_assistant');
49
50        return new self(
51            workers: $config->readInt('workers'),
52            logLevel: $config->readParsedValue('log_level', LogLevel::from(...)),
53            logFormat: $config->readParsedValue('log_format', LogFormat::from(...)),
54            shutdownGrace: $config->readDuration('shutdown_grace'),
55            workerEventBuffer: $config->readInt('worker_event_buffer'),
56            workerStateBatch: $config->readInt('worker_state_batch'),
57            subscriptionBuffer: $config->readInt('subscription_buffer'),
58            homeAssistant: $homeAssistant === null ? null : self::readConnectionConfig($homeAssistant),
59            reconnectBackoff: $config->readSection('reconnect')->readBackoff(''),
60            supervision: SupervisionConfig::fromSection($config->readSection('supervision')),
61            serviceCalls: ServiceCallPolicy::fromSection($config->readSection('service_calls')),
62            persistence: PersistenceConfig::fromSection($config->readSection('persistence')),
63            mqtt: MqttConfig::fromSection($config->readSection('mqtt')),
64            control: ControlConfig::fromSection($config->readSection('control')),
65            http: HttpConfig::fromSection($config->readSection('http')),
66            codegen: CodegenConfig::fromSection($config->readSection('codegen')),
67            gitDeploy: GitDeployConfig::fromSection($config->readSection('deploy')->readSection('git')),
68            expose: ExposeConfig::fromSection($config->readSection('expose')),
69            apps: AppOverrideCollection::keyedByAppId($config->readSection('apps')->mapSubsections(AppOverride::fromSection(...))),
70        );
71    }
72
73    /** @throws ConfigurationException */
74    public function requireHomeAssistant(): ConnectionConfig
75    {
76        return $this->homeAssistant ?? throw ConfigurationException::createForReason(ConfigurationError::HomeAssistantMissing);
77    }
78
79    /** @throws ConfigurationException */
80    private static function readConnectionConfig(#[SensitiveParameter] ConfigSection $homeAssistant): ConnectionConfig
81    {
82        $oneMillisecond = Duration::milliseconds(1);
83
84        return new ConnectionConfig(
85            url: $homeAssistant->readParsedValue('url', HomeAssistantUrl::parse(...)),
86            token: $homeAssistant->readString('token'),
87            connectTimeout: $homeAssistant->readDuration('connect_timeout', $oneMillisecond),
88            commandTimeout: $homeAssistant->readDuration('command_timeout', $oneMillisecond),
89            heartbeatInterval: $homeAssistant->readOptionalDuration('heartbeat_interval', Duration::seconds(1))->findDuration(),
90            messageSizeLimit: $homeAssistant->readInt('message_size_limit'),
91            frameSizeLimit: $homeAssistant->readInt('frame_size_limit'),
92            heartbeatMissedLimit: $homeAssistant->readInt('heartbeat_missed_limit'),
93        );
94    }
95}