Lines 90.00% 9 / 10
Functions and Methods 88.88% 8 / 9
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
RecordingExposedSwitch 90.00% 9 / 10 88.88% 8 / 9 10.10 0.00% 0 / 1
 getConfig 0.00% 0 / 1 0.00% 0 / 1 2
 updateConfig 100.00% 1 / 1 100.00% 1 / 1 1
 getValue 100.00% 2 / 2 100.00% 1 / 1 2
 setOn 100.00% 1 / 1 100.00% 1 / 1 1
 setOff 100.00% 1 / 1 100.00% 1 / 1 1
 watchCommands 100.00% 1 / 1 100.00% 1 / 1 1
 pushCommand 100.00% 1 / 1 100.00% 1 / 1 1
 pushTurnOn 100.00% 1 / 1 100.00% 1 / 1 1
 pushTurnOff 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Testing\Exposure;
6
7use Stewart\Contracts\EventStream;
8use Stewart\Contracts\Exception\CommandException;
9use Stewart\Contracts\Exposure\Command\SwitchAction;
10use Stewart\Contracts\Exposure\Command\SwitchCommand;
11use Stewart\Contracts\Exposure\ExposedState;
12use Stewart\Contracts\Exposure\ExposedStateChange;
13use Stewart\Contracts\Exposure\ExposedSwitch;
14use Stewart\Contracts\Exposure\SwitchConfig;
15use Stewart\Contracts\State\EventContext;
16
17/** @extends RecordingExposedEntity<SwitchConfig> */
18final class RecordingExposedSwitch extends RecordingExposedEntity implements ExposedSwitch
19{
20    public function getConfig(): SwitchConfig
21    {
22        return $this->config;
23    }
24
25    public function updateConfig(SwitchConfig $config): void
26    {
27        $this->recordConfig($config);
28    }
29
30    public function getValue(): ?bool
31    {
32        $value = $this->findStateValue();
33
34        return \is_bool($value) ? $value : null;
35    }
36
37    public function setOn(?array $attributes = null): void
38    {
39        $this->recordChange(new ExposedStateChange(new ExposedState(true), $attributes));
40    }
41
42    public function setOff(?array $attributes = null): void
43    {
44        $this->recordChange(new ExposedStateChange(new ExposedState(false), $attributes));
45    }
46
47    public function watchCommands(): EventStream
48    {
49        /** @var EventStream<SwitchCommand> */
50        return $this->watchPushedCommands();
51    }
52
53    /** @throws CommandException */
54    public function pushCommand(SwitchCommand $command): void
55    {
56        $this->deliverCommand($command);
57    }
58
59    /** @throws CommandException */
60    public function pushTurnOn(?EventContext $context = null): void
61    {
62        $this->pushCommand(new SwitchCommand(SwitchAction::TurnOn, $context ?? EventContext::unknown()));
63    }
64
65    /** @throws CommandException */
66    public function pushTurnOff(?EventContext $context = null): void
67    {
68        $this->pushCommand(new SwitchCommand(SwitchAction::TurnOff, $context ?? EventContext::unknown()));
69    }
70}