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

StructureFactory::fromData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 12
ccs 11
cts 11
cp 1
crap 1
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\NestApi\Client\Factory;
6
7
use DateTimeZone;
8
use LauLamanApps\NestApi\Client\__shared\AbstractFactory;
9
use LauLamanApps\NestApi\Client\Structure;
10
use LauLamanApps\NestApi\Client\Structure\Away;
11
use LauLamanApps\NestApi\NestClientInterface;
12
13
final class StructureFactory extends AbstractFactory implements StructureFactoryInterface
14
{
15 4
    public function fromData(array $data, NestClientInterface $client): Structure
16
    {
17 4
        return new Structure(
18 4
            $this->extractString('structure_id', $data),
19 4
            $this->extractString('name', $data),
20 4
            $this->extractThermostats($client, $this->extractArrayOrNull('thermostats', $data)),
21 4
            $this->extractProtects($client, $this->extractArrayOrNull('smoke_co_alarms', $data)),
22 4
            $this->extractCameras($client, $this->extractArrayOrNull('cameras', $data)),
23 4
            $this->extractString('country_code', $data),
24 4
            new DateTimeZone($this->extractString('time_zone', $data)),
25 4
            Away::get($this->extractString('away', $data)),
26 4
            $this->extractBoolean('rhr_enrollment', $data)
27
        );
28
    }
29
30 4
    private function extractThermostats(NestClientInterface $client, ?array $thermostatsData): array
31
    {
32 4
        $thermostatsData = $thermostatsData ?? [];
33 4
        $thermostats = [];
34 4
        foreach ($thermostatsData as $thermostat) {
35 2
            $thermostats[$thermostat] = new Structure\ThermostatProxy($client, $thermostat);
36
        }
37
38 4
        return $thermostats;
39
    }
40
41 4
    private function extractProtects(NestClientInterface $client, ?array $protectsData): array
42
    {
43 4
        $protectsData = $protectsData ?? [];
44 4
        $protects = [];
45 4
        foreach ($protectsData as $protect) {
46 2
            $protects[$protect] = new Structure\ProtectProxy($client, $protect);
47
        }
48
49 4
        return $protects;
50
    }
51
52 4
    private function extractCameras(NestClientInterface $client, ?array $camerasData): array
53
    {
54 4
        $camerasData = $camerasData ?? [];
55 4
        $cameras = [];
56 4
        foreach ($camerasData as $camera) {
57 2
            $cameras[$camera] = new Structure\CameraProxy($client, $camera);
58
        }
59
60 4
        return $cameras;
61
    }
62
}
63