Passed
Push — master ( 7cd6c5...dd963e )
by Laurens
04:05
created

AddressFactory::extractRijksDriehoek()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 2
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\KvkApi\Client\Factory\Profile\Company;
6
7
use Werkspot\KvkApi\Client\Factory\AbstractFactory;
8
use Werkspot\KvkApi\Client\Profile\Company\Address;
9
use Werkspot\KvkApi\Client\Profile\Company\Address\GpsCoordinates;
10
use Werkspot\KvkApi\Client\Profile\Company\Address\RijksDriehoek;
11
12
final class AddressFactory extends AbstractFactory implements AddressFactoryInterface
13
{
14
    /**
15
     * @return Address[]
16
     */
17 16
    public function fromArray(array $data): array
18
    {
19 16
        $addresses = [];
20 16
        foreach ($data as $address) {
21 16
            $addresses[] = $this->buildFromArray($address);
22
        }
23
24 16
        return $addresses;
25
    }
26
27 16
    private function buildFromArray($data): Address
28
    {
29 16
        return new Address(
30 16
            $this->extractStringOrNull('type', $data),
31 16
            $this->extractStringOrNull('bagId', $data),
32 16
            $this->extractStringOrNull('street', $data),
33 16
            $this->extractStringOrNull('houseNumber', $data),
34 16
            $this->extractStringOrNull('houseNumberAddition', $data),
35 16
            $this->extractStringOrNull('postalCode', $data),
36 16
            $this->extractStringOrNull('city', $data),
37 16
            $this->extractStringOrNull('country', $data),
38 16
            $this->extractGpsCoordinates($data),
39 16
            $this->extractRijksDriehoek($data)
40
        );
41
    }
42
43 16
    private function extractGpsCoordinates(array $data): ?GpsCoordinates
44
    {
45 16
        if (array_key_exists('gpsLatitude', $data) && array_key_exists('gpsLongitude', $data)) {
46 16
            return new GpsCoordinates(
47 16
                $this->extractFloatOrNull('gpsLatitude', $data),
48 16
                $this->extractFloatOrNull('gpsLongitude', $data)
49
            );
50
        }
51
52 5
        return null;
53
    }
54
55 16
    private function extractRijksDriehoek(array $data): ?RijksDriehoek
56
    {
57
        if (
58 16
            array_key_exists('rijksdriehoekX', $data) &&
59 16
            array_key_exists('rijksdriehoekY', $data) &&
60 16
            array_key_exists('rijksdriehoekZ', $data)
61
        ) {
62 16
            return new RijksDriehoek(
63 16
                $this->extractFloatOrNull('rijksdriehoekX', $data),
64 16
                $this->extractFloatOrNull('rijksdriehoekY', $data),
65 16
                $this->extractFloatOrNull('rijksdriehoekZ', $data)
66
            );
67
        }
68
69 5
        return null;
70
    }
71
}
72