Lines 100.00% 7 / 7
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ComponentVersion 100.00% 7 / 7 100.00% 3 / 3 5 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromVersionResult 100.00% 5 / 5 100.00% 1 / 1 3
 isCompatible 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Client\Component;
6
7use Stewart\Client\Exception\HaClientException;
8
9final readonly class ComponentVersion
10{
11    public function __construct(
12        public string $componentVersion,
13        public int $protocol,
14    ) {}
15
16    /**
17     * @param array<array-key, mixed> $result
18     * @throws HaClientException
19     */
20    public static function fromVersionResult(array $result): self
21    {
22        $componentVersion = $result['component_version'] ?? null;
23        $protocol = $result['protocol'] ?? null;
24
25        if (!\is_string($componentVersion) || !\is_int($protocol)) {
26            throw HaClientException::protocolViolation('a stewart/version result without component_version and protocol');
27        }
28
29        return new self($componentVersion, $protocol);
30    }
31
32    public function isCompatible(): bool
33    {
34        return $this->protocol === ComponentProtocol::VERSION;
35    }
36}