1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service\DistanceMatrix; |
13
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Message\MessageFactory; |
16
|
|
|
use Ivory\GoogleMap\Service\AbstractService; |
17
|
|
|
use Ivory\GoogleMap\Service\Base\Distance; |
18
|
|
|
use Ivory\GoogleMap\Service\Base\Duration; |
19
|
|
|
use Ivory\GoogleMap\Service\Base\Fare; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class DistanceMatrix extends AbstractService |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param HttpClient $client |
28
|
|
|
* @param MessageFactory $messageFactory |
29
|
|
|
*/ |
30
|
|
|
public function __construct(HttpClient $client, MessageFactory $messageFactory) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/distancematrix'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param DistanceMatrixRequest $request |
37
|
|
|
* |
38
|
|
|
* @return DistanceMatrixResponse |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function process(DistanceMatrixRequest $request) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$response = $this->getClient()->sendRequest($this->createRequest($request->buildQuery())); |
43
|
|
|
$data = $this->parse((string) $response->getBody()); |
44
|
|
|
|
45
|
|
|
return $this->buildResponse($data); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $data |
50
|
|
|
* |
51
|
|
|
* @return mixed[] |
52
|
|
|
*/ |
53
|
|
|
private function parse($data) |
54
|
|
|
{ |
55
|
|
|
if ($this->getFormat() === self::FORMAT_JSON) { |
56
|
|
|
return json_decode($data, true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->getXmlParser()->parse($data, [ |
60
|
|
|
'destination_address' => 'destination_addresses', |
61
|
|
|
'element' => 'elements', |
62
|
|
|
'origin_address' => 'origin_addresses', |
63
|
|
|
'row' => 'rows', |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed[] $data |
69
|
|
|
* |
70
|
|
|
* @return DistanceMatrixResponse |
71
|
|
|
*/ |
72
|
|
|
private function buildResponse(array $data) |
73
|
|
|
{ |
74
|
|
|
$response = new DistanceMatrixResponse(); |
75
|
|
|
$response->setStatus($data['status']); |
76
|
|
|
$response->setDestinations($data['destination_addresses']); |
77
|
|
|
$response->setOrigins($data['origin_addresses']); |
78
|
|
|
$response->setRows($this->buildRows($data['rows'])); |
79
|
|
|
|
80
|
|
|
return $response; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed[] $data |
85
|
|
|
* |
86
|
|
|
* @return DistanceMatrixRow[] |
87
|
|
|
*/ |
88
|
|
|
private function buildRows($data) |
89
|
|
|
{ |
90
|
|
|
$rows = []; |
91
|
|
|
|
92
|
|
|
foreach ($data as $item) { |
93
|
|
|
$rows[] = $this->buildRow($item); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $rows; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param mixed[] $data |
101
|
|
|
* |
102
|
|
|
* @return DistanceMatrixRow |
103
|
|
|
*/ |
104
|
|
|
private function buildRow($data) |
105
|
|
|
{ |
106
|
|
|
$elements = []; |
107
|
|
|
foreach ($data['elements'] as $element) { |
108
|
|
|
$elements[] = $this->buildElement($element); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$row = new DistanceMatrixRow(); |
112
|
|
|
$row->setElements($elements); |
113
|
|
|
|
114
|
|
|
return $row; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param mixed[] $data |
119
|
|
|
* |
120
|
|
|
* @return DistanceMatrixElement |
121
|
|
|
*/ |
122
|
|
|
private function buildElement(array $data) |
123
|
|
|
{ |
124
|
|
|
$element = new DistanceMatrixElement(); |
125
|
|
|
$element->setStatus($data['status']); |
126
|
|
|
$element->setDistance(isset($data['distance']) ? $this->buildDistance($data['distance']) : null); |
127
|
|
|
$element->setDuration(isset($data['duration']) ? $this->buildDuration($data['duration']) : null); |
128
|
|
|
$element->setFare(isset($data['fare']) ? $this->buildFare($data['fare']) : null); |
129
|
|
|
$element->setDurationInTraffic( |
130
|
|
|
isset($data['duration_in_traffic']) ? $this->buildDuration($data['duration_in_traffic']) : null |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
return $element; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @codeCoverageIgnore |
138
|
|
|
* |
139
|
|
|
* @param mixed[] $data |
140
|
|
|
* |
141
|
|
|
* @return Fare |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
private function buildFare(array $data) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$fare = new Fare(); |
146
|
|
|
$fare->setCurrency($data['currency']); |
147
|
|
|
$fare->setValue($data['value']); |
148
|
|
|
$fare->setText($data['text']); |
149
|
|
|
|
150
|
|
|
return $fare; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param mixed[] $data |
155
|
|
|
* |
156
|
|
|
* @return Distance |
157
|
|
|
*/ |
158
|
|
|
private function buildDistance(array $data) |
159
|
|
|
{ |
160
|
|
|
return new Distance($data['text'], $data['value']); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param mixed[] $data |
165
|
|
|
* |
166
|
|
|
* @return Duration |
167
|
|
|
*/ |
168
|
|
|
private function buildDuration(array $data) |
169
|
|
|
{ |
170
|
|
|
return new Duration($data['text'], $data['value']); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.