1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace LauLamanApps\NestApi; |
||||||
6 | |||||||
7 | use LauLamanApps\NestApi\Client\Device\Camera; |
||||||
8 | use LauLamanApps\NestApi\Client\Device\SmokeCoAlarm; |
||||||
9 | use LauLamanApps\NestApi\Client\Device\Thermostat; |
||||||
10 | use LauLamanApps\NestApi\Client\Factory\Device\CameraFactoryInterface; |
||||||
11 | use LauLamanApps\NestApi\Client\Factory\Device\SmokeCoAlarmFactoryInterface; |
||||||
12 | use LauLamanApps\NestApi\Client\Factory\Device\ThermostatFactoryInterface; |
||||||
13 | use LauLamanApps\NestApi\Client\Factory\StructureFactoryInterface; |
||||||
14 | use LauLamanApps\NestApi\Client\Structure; |
||||||
15 | use LauLamanApps\NestApi\Http\ClientInterface; |
||||||
16 | use LauLamanApps\NestApi\Http\Command\Command; |
||||||
17 | use LauLamanApps\NestApi\Http\Command\ThermostatCommand; |
||||||
18 | use LauLamanApps\NestApi\Http\Endpoint\MapperInterface; |
||||||
19 | |||||||
20 | final class NestClient implements NestClientInterface |
||||||
21 | { |
||||||
22 | /** |
||||||
23 | * @var ClientInterface |
||||||
24 | */ |
||||||
25 | private $httpClient; |
||||||
26 | |||||||
27 | /** |
||||||
28 | * @var ThermostatFactoryInterface |
||||||
29 | */ |
||||||
30 | private $thermostatFactory; |
||||||
31 | |||||||
32 | /** |
||||||
33 | * @var SmokeCoAlarmFactoryInterface |
||||||
34 | */ |
||||||
35 | private $SmokeCoAlarmFactory; |
||||||
36 | |||||||
37 | /** |
||||||
38 | * @var CameraFactoryInterface |
||||||
39 | */ |
||||||
40 | private $cameraFactory; |
||||||
41 | |||||||
42 | /** |
||||||
43 | * @var StructureFactoryInterface |
||||||
44 | */ |
||||||
45 | private $structureFactory; |
||||||
46 | |||||||
47 | 8 | public function __construct( |
|||||
48 | ClientInterface $httpClient, |
||||||
49 | ThermostatFactoryInterface $thermostatFactory, |
||||||
50 | SmokeCoAlarmFactoryInterface $SmokeCoAlarmFactory, |
||||||
51 | CameraFactoryInterface $cameraFactory, |
||||||
52 | StructureFactoryInterface $structureFactory |
||||||
53 | ) { |
||||||
54 | 8 | $this->httpClient = $httpClient; |
|||||
55 | 8 | $this->thermostatFactory = $thermostatFactory; |
|||||
56 | 8 | $this->SmokeCoAlarmFactory = $SmokeCoAlarmFactory; |
|||||
57 | 8 | $this->cameraFactory = $cameraFactory; |
|||||
58 | 8 | $this->structureFactory = $structureFactory; |
|||||
59 | 8 | } |
|||||
60 | |||||||
61 | /** |
||||||
62 | * @return Thermostat[] |
||||||
63 | */ |
||||||
64 | 1 | public function getThermostats(): array |
|||||
65 | { |
||||||
66 | 1 | $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::THERMOSTATS)); |
|||||
67 | 1 | $data = $this->decodeJsonToArray($json); |
|||||
68 | |||||||
69 | 1 | $thermostats = []; |
|||||
70 | 1 | foreach ($data as $id => $thermostatData) { |
|||||
71 | 1 | $thermostats[] = $this->thermostatFactory->fromData($thermostatData, $this); |
|||||
72 | } |
||||||
73 | |||||||
74 | 1 | return $thermostats; |
|||||
75 | } |
||||||
76 | |||||||
77 | 1 | public function getThermostat(string $id): Thermostat |
|||||
78 | { |
||||||
79 | 1 | $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::THERMOSTAT, [$id])); |
|||||
80 | 1 | $data = $this->decodeJsonToArray($json); |
|||||
81 | |||||||
82 | 1 | return $this->thermostatFactory->fromData($data, $this); |
|||||
83 | } |
||||||
84 | |||||||
85 | /** |
||||||
86 | * @return SmokeCoAlarm[] |
||||||
87 | */ |
||||||
88 | 1 | public function getSmokeCoAlarms(): array |
|||||
89 | { |
||||||
90 | 1 | $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::SmokeCoAlarmS)); |
|||||
91 | 1 | $data = $this->decodeJsonToArray($json); |
|||||
92 | |||||||
93 | 1 | $SmokeCoAlarms = []; |
|||||
94 | |||||||
95 | 1 | foreach ($data as $id => $SmokeCoAlarmData) { |
|||||
96 | 1 | $SmokeCoAlarms[] = $this->SmokeCoAlarmFactory->fromData($SmokeCoAlarmData, $this); |
|||||
0 ignored issues
–
show
|
|||||||
97 | } |
||||||
98 | |||||||
99 | 1 | return $SmokeCoAlarms; |
|||||
100 | } |
||||||
101 | |||||||
102 | 1 | public function getSmokeCoAlarm(string $id): SmokeCoAlarm |
|||||
103 | { |
||||||
104 | 1 | $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::SmokeCoAlarm, [$id])); |
|||||
105 | 1 | $data = $this->decodeJsonToArray($json); |
|||||
106 | |||||||
107 | 1 | return $this->SmokeCoAlarmFactory->fromData($data, $this); |
|||||
0 ignored issues
–
show
The call to
LauLamanApps\NestApi\Cli...ryInterface::fromData() has too many arguments starting with $this .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
108 | } |
||||||
109 | |||||||
110 | /** |
||||||
111 | * @return Camera[] |
||||||
112 | */ |
||||||
113 | 1 | public function getCameras(): array |
|||||
114 | { |
||||||
115 | 1 | $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::CAMERAS)); |
|||||
116 | 1 | $data = $this->decodeJsonToArray($json); |
|||||
117 | |||||||
118 | 1 | $cameras = []; |
|||||
119 | 1 | foreach ($data as $id => $cameraData) { |
|||||
120 | 1 | $cameras[] = $this->cameraFactory->fromData($cameraData, $this); |
|||||
0 ignored issues
–
show
The call to
LauLamanApps\NestApi\Cli...ryInterface::fromData() has too many arguments starting with $this .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
121 | } |
||||||
122 | |||||||
123 | 1 | return $cameras; |
|||||
124 | } |
||||||
125 | |||||||
126 | 1 | public function getCamera(string $id): Camera |
|||||
127 | { |
||||||
128 | 1 | $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::CAMERA, [$id])); |
|||||
129 | 1 | $data = $this->decodeJsonToArray($json); |
|||||
130 | |||||||
131 | 1 | return $this->cameraFactory->fromData($data, $this); |
|||||
0 ignored issues
–
show
The call to
LauLamanApps\NestApi\Cli...ryInterface::fromData() has too many arguments starting with $this .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
132 | } |
||||||
133 | |||||||
134 | 7 | private function decodeJsonToArray(string $json): array |
|||||
135 | { |
||||||
136 | 7 | return json_decode($json, true); |
|||||
137 | } |
||||||
138 | |||||||
139 | /** |
||||||
140 | * @return Structure[] |
||||||
141 | */ |
||||||
142 | 1 | public function getStructures(): array |
|||||
143 | { |
||||||
144 | 1 | $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::STRUCTURES)); |
|||||
145 | 1 | $data = $this->decodeJsonToArray($json); |
|||||
146 | |||||||
147 | 1 | $structures = []; |
|||||
148 | 1 | foreach ($data as $structureData) { |
|||||
149 | 1 | $structures[] = $this->structureFactory->fromData($structureData, $this); |
|||||
150 | } |
||||||
151 | |||||||
152 | 1 | return $structures; |
|||||
153 | } |
||||||
154 | |||||||
155 | 1 | public function sendCommand(Command $command): void |
|||||
156 | { |
||||||
157 | 1 | if ($command instanceof ThermostatCommand) { |
|||||
158 | 1 | $this->httpClient->putEndpoint( |
|||||
159 | 1 | MapperInterface::THERMOSTAT_PUT, |
|||||
160 | 1 | [$command->getDeviceId()], |
|||||
161 | 1 | $command->getCommands() |
|||||
162 | ); |
||||||
163 | } |
||||||
164 | 1 | } |
|||||
165 | } |
||||||
166 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.