Passed
Push — master ( f9c14b...c6fe8b )
by Laurens
02:03
created

NestClient::getProtect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Factory\Device\CameraFactoryInterface;
9
use LauLamanApps\NestApi\Client\Factory\Device\ProtectFactoryInterface;
10
use LauLamanApps\NestApi\Client\Factory\Device\ThermostatFactoryInterface;
11
use LauLamanApps\NestApi\Client\Device\Protect;
12
use LauLamanApps\NestApi\Client\Device\Thermostat;
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 ProtectFactoryInterface
34
     */
35
    private $protectFactory;
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
        ProtectFactoryInterface $protectFactory,
51
        CameraFactoryInterface $cameraFactory,
52
        StructureFactoryInterface $structureFactory
53
    ) {
54 8
        $this->httpClient = $httpClient;
55 8
        $this->thermostatFactory = $thermostatFactory;
56 8
        $this->protectFactory = $protectFactory;
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 Protect[]
87
     */
88 1
    public function getProtects(): array
89
    {
90 1
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::PROTECTS));
91 1
        $data = $this->decodeJsonToArray($json);
92
93 1
        $protects = [];
94
95 1
        foreach ($data as $id => $protectData) {
96 1
            $protects[] = $this->protectFactory->fromData($protectData, $this);
97
        }
98
99 1
        return $protects;
100
    }
101
102 1
    public function getProtect(string $id): Protect
103
    {
104 1
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::PROTECT, [$id]));
105 1
        $data = $this->decodeJsonToArray($json);
106
107 1
        return $this->protectFactory->fromData($data, $this);
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);
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);
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->getId()],
161 1
                $command->getCommands()
162
            );
163
        }
164 1
    }
165
}
166