DistanceMatrixResponse::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace DH\NavigationBundle\Provider\GoogleMaps\DistanceMatrix;
4
5
use DH\NavigationBundle\Contract\DistanceMatrix\DistanceMatrixResponseInterface;
6
use DH\NavigationBundle\Model\Address;
7
use DH\NavigationBundle\Model\Distance;
8
use DH\NavigationBundle\Model\DistanceMatrix\Element;
9
use DH\NavigationBundle\Model\DistanceMatrix\Row;
10
use DH\NavigationBundle\Model\Duration;
11
use Psr\Http\Message\ResponseInterface;
12
use stdClass;
13
14
class DistanceMatrixResponse implements DistanceMatrixResponseInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $status;
20
21
    /**
22
     * @var stdClass
23
     */
24
    private $responseObject;
25
26
    /**
27
     * @var Address[]
28
     */
29
    private $originAddresses;
30
31
    /**
32
     * @var Address[]
33
     */
34
    private $destinationAddresses;
35
36
    /**
37
     * @var Row[]
38
     */
39
    private $rows;
40
41
    public function __construct(ResponseInterface $response)
42
    {
43
        $responseObject = json_decode($response->getBody()->getContents());
44
        $this->responseObject = $responseObject;
45
        $this->originAddresses = [];
46
        $this->destinationAddresses = [];
47
        $this->rows = [];
48
        $this->initialize();
49
    }
50
51
    private function addOriginAddress(Address $originAddress): void
52
    {
53
        $this->originAddresses[] = $originAddress;
54
    }
55
56
    private function addDestinationAddress(Address $destinationAddress): void
57
    {
58
        $this->destinationAddresses[] = $destinationAddress;
59
    }
60
61
    private function addRow(Row $row): void
62
    {
63
        $this->rows[] = $row;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getStatus(): string
70
    {
71
        return $this->status;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getResponseObject(): stdClass
78
    {
79
        return $this->responseObject;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getOriginAddresses(): array
86
    {
87
        return $this->originAddresses;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getDestinationAddresses(): array
94
    {
95
        return $this->destinationAddresses;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getRows(): array
102
    {
103
        return $this->rows;
104
    }
105
106
    /**
107
     * @throws \Exception
108
     */
109
    private function initialize(): void
110
    {
111
        $this->status = $this->responseObject->status;
112
113
        foreach ($this->responseObject->origin_addresses as $originAddress) {
114
            $this->addOriginAddress(new Address($originAddress));
115
        }
116
117
        foreach ($this->responseObject->destination_addresses as $destinationAddress) {
118
            $this->addDestinationAddress(new Address($destinationAddress));
119
        }
120
121
        foreach ($this->responseObject->rows as $row) {
122
            $elements = [];
123
            foreach ($row->elements as $element) {
124
                if (property_exists($element, 'status') && Element::STATUS_OK !== $element->status) {
125
                    $status = $element->status;
126
                    $distance = null;
127
                    $duration = null;
128
                } else {
129
                    $status = 'OK';
130
                    $distance = new Distance((int) $element->distance->value);
131
                    $duration = new Duration((int) $element->duration->value);
132
                }
133
134
                $elements[] = new Element($status, $duration, $distance);
135
            }
136
            $this->addRow(new Row($elements));
137
        }
138
    }
139
}
140