Lines 100.00% 31 / 31
Methods 100.00% 9 / 9
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 readExposedCommand 100.00% 13 / 13 100.00% 1 / 1 22
 readNumberCommand 100.00% 2 / 2 100.00% 1 / 1 3
 readSelectCommand 100.00% 2 / 2 100.00% 1 / 1 2
 readTextCommand 100.00% 2 / 2 100.00% 1 / 1 2
 readTimeCommand 100.00% 3 / 3 100.00% 1 / 1 3
 readDateCommand 100.00% 3 / 3 100.00% 1 / 1 3
 readDateTimeCommand 100.00% 3 / 3 100.00% 1 / 1 3
 readStringValue 100.00% 2 / 2 100.00% 1 / 1 2
24final readonly class ComponentCommand implements ComponentSessionEvent
25{
26    /** @param array<string, mixed> $data */
27    public function __construct(
28        public string $commandId,
29        public AppId $appId,
30        public ExposedEntityKey $key,
31        public ComponentCommandAction $action,
32        public array $data,
33        public EventContext $context,
34    ) {}
35
36    // Null when the action or its data does not fit the platform.
37    public function readExposedCommand(ExposedPlatform $platform): ?ExposedCommand
38    {
39        return match ($platform) {
40            ExposedPlatform::Switch => match ($this->action) {
41                ComponentCommandAction::TurnOn => new SwitchCommand(SwitchAction::TurnOn, $this->context),
42                ComponentCommandAction::TurnOff => new SwitchCommand(SwitchAction::TurnOff, $this->context),
43                default => null,
44            },
45            ExposedPlatform::Button => $this->action === ComponentCommandAction::Press ? new ButtonPress($this->context) : null,
46            ExposedPlatform::Number => $this->action === ComponentCommandAction::SetValue ? $this->readNumberCommand() : null,
47            ExposedPlatform::Select => $this->action === ComponentCommandAction::SelectOption ? $this->readSelectCommand() : null,
48            ExposedPlatform::Text => $this->action === ComponentCommandAction::SetValue ? $this->readTextCommand() : null,
49            ExposedPlatform::Time => $this->action === ComponentCommandAction::SetValue ? $this->readTimeCommand() : null,
50            ExposedPlatform::Date => $this->action === ComponentCommandAction::SetValue ? $this->readDateCommand() : null,
51            ExposedPlatform::DateTime => $this->action === ComponentCommandAction::SetValue ? $this->readDateTimeCommand() : null,
52            ExposedPlatform::Action => $this->action === ComponentCommandAction::Call ? new ActionCall($this->data, $this->context) : null,
53            ExposedPlatform::Sensor, ExposedPlatform::BinarySensor => null,
54        };
55    }
56
57    private function readNumberCommand(): ?NumberCommand
58    {
59        $value = $this->data['value'] ?? null;
60
61        return \is_int($value) || \is_float($value) ? new NumberCommand($value, $this->context) : null;
62    }
63
64    private function readSelectCommand(): ?SelectCommand
65    {
66        $option = $this->data['option'] ?? null;
67
68        return \is_string($option) ? new SelectCommand($option, $this->context) : null;
69    }
70
71    private function readTextCommand(): ?TextCommand
72    {
73        $value = $this->readStringValue();
74
75        return $value === null ? null : new TextCommand($value, $this->context);
76    }
77
78    private function readTimeCommand(): ?TimeCommand
79    {
80        $value = $this->readStringValue();
81        $time = $value === null ? null : CalendarStateFormat::parseTime($value);
82
83        return $time === null ? null : new TimeCommand($time, $this->context);
84    }
85
86    private function readDateCommand(): ?DateCommand
87    {
88        $value = $this->readStringValue();
89        $date = $value === null ? null : CalendarStateFormat::parseDate($value);
90
91        return $date === null ? null : new DateCommand($date, $this->context);
92    }
93
94    private function readDateTimeCommand(): ?DateTimeCommand
95    {
96        $value = $this->readStringValue();
97        $moment = $value === null ? null : CalendarStateFormat::parseDateTime($value);
98
99        return $moment === null ? null : new DateTimeCommand($moment, $this->context);
100    }
101
102    private function readStringValue(): ?string
103    {
104        $value = $this->data['value'] ?? null;
105
106        return \is_string($value) ? $value : null;
107    }
108}