Lines 100.00% 6 / 6
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
HaUser 100.00% 6 / 6 100.00% 2 / 2 5 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromCurrentUserResult 100.00% 5 / 5 100.00% 1 / 1 4
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Client;
6
7use Stewart\Client\Exception\HaClientException;
8
9final readonly class HaUser
10{
11    public function __construct(
12        public string $id,
13        public ?string $name,
14        public bool $isAdmin,
15    ) {}
16
17    /**
18     * @param array<array-key, mixed> $result
19     * @throws HaClientException
20     */
21    public static function fromCurrentUserResult(array $result): self
22    {
23        $id = $result['id'] ?? null;
24        $name = $result['name'] ?? null;
25
26        if (!\is_string($id) || $id === '') {
27            throw HaClientException::protocolViolation('a current user without an id');
28        }
29
30        return new self($id, \is_string($name) ? $name : null, ($result['is_admin'] ?? false) === true);
31    }
32}