Lines 90.90% 10 / 11
Functions and Methods 75.00% 3 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SubscribeTrigger 90.90% 10 / 11 75.00% 3 / 4 5.02 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 type 0.00% 0 / 1 0.00% 0 / 1 2
 describe 100.00% 2 / 2 100.00% 1 / 1 1
 toMessage 100.00% 7 / 7 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Client\Connection\Command;
6
7use Stewart\Contracts\Trigger\Collection\HaTriggerCollection;
8use Stewart\Contracts\Trigger\HaTrigger;
9
10final readonly class SubscribeTrigger implements SubscriptionCommand
11{
12    /** @param array<string, mixed> $variables */
13    public function __construct(
14        public HaTriggerCollection $triggers,
15        public array $variables = [],
16    ) {}
17
18    public function type(): string
19    {
20        return 'subscribe_trigger';
21    }
22
23    public function describe(): string
24    {
25        $platforms = $this->triggers->mapToList(static fn(HaTrigger $trigger): string => $trigger->getPlatform());
26
27        return \sprintf('subscribe_trigger "%s"', implode(',', $platforms));
28    }
29
30    public function toMessage(): array
31    {
32        $message = [
33            'type' => $this->type(),
34            'trigger' => $this->triggers->mapToList(static fn(HaTrigger $trigger): array => $trigger->toArray()),
35        ];
36
37        if ($this->variables !== []) {
38            $message['variables'] = $this->variables;
39        }
40
41        return $message;
42    }
43}