DistanceMatrixResponse::addOriginAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DH\NavigationBundle\Provider\Here\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, array $origins, array $destinations)
42
    {
43
        $responseObject = json_decode($response->getBody()->getContents());
44
        $this->responseObject = $responseObject;
45
        $this->originAddresses = [];
46
        $this->destinationAddresses = [];
47
        $this->rows = [];
48
        $this->status = $response->getReasonPhrase();
49
50
        foreach ($origins as $origin) {
51
            $this->addOriginAddress(new Address($origin));
52
        }
53
54
        foreach ($destinations as $destination) {
55
            $this->addDestinationAddress(new Address($destination));
56
        }
57
58
        $this->initialize();
59
    }
60
61
    private function addOriginAddress(Address $originAddress): void
62
    {
63
        $this->originAddresses[] = $originAddress;
64
    }
65
66
    private function addDestinationAddress(Address $destinationAddress): void
67
    {
68
        $this->destinationAddresses[] = $destinationAddress;
69
    }
70
71
    private function addRow(Row $row): void
72
    {
73
        $this->rows[] = $row;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getStatus(): string
80
    {
81
        return $this->status;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getResponseObject(): stdClass
88
    {
89
        return $this->responseObject;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getOriginAddresses(): array
96
    {
97
        return $this->originAddresses;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getDestinationAddresses(): array
104
    {
105
        return $this->destinationAddresses;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getRows(): array
112
    {
113
        return $this->rows;
114
    }
115
116
    /**
117
     * @throws \Exception
118
     */
119
    private function initialize(): void
120
    {
121
        $startIndex = 0;
122
        $elements = [];
123
        foreach ($this->responseObject->response->matrixEntry 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->summary->distance);
131
                $duration = new Duration((int) $element->summary->travelTime);
132
            }
133
134
            if ($startIndex !== $element->startIndex) {
135
                $this->addRow(new Row($elements));
136
                $startIndex = $element->startIndex;
137
                $elements = [];
138
            }
139
140
            $elements[] = new Element($status, $duration, $distance);
141
        }
142
        $this->addRow(new Row($elements));
143
    }
144
}
145