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\Elevation; |
13
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Message\MessageFactory; |
16
|
|
|
use Ivory\GoogleMap\Base\Coordinate; |
17
|
|
|
use Ivory\GoogleMap\Service\AbstractService; |
18
|
|
|
use Ivory\GoogleMap\Service\Elevation\Request\ElevationRequestInterface; |
19
|
|
|
use Ivory\GoogleMap\Service\Elevation\Response\ElevationResponse; |
20
|
|
|
use Ivory\GoogleMap\Service\Elevation\Response\ElevationResult; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author GeLo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ElevationService extends AbstractService |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param HttpClient $client |
29
|
|
|
* @param MessageFactory $messageFactory |
30
|
|
|
*/ |
31
|
|
|
public function __construct(HttpClient $client, MessageFactory $messageFactory) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/elevation'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ElevationRequestInterface $request |
38
|
|
|
* |
39
|
|
|
* @return ElevationResponse |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function process(ElevationRequestInterface $request) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$response = $this->getClient()->sendRequest($this->createRequest($request->build())); |
44
|
|
|
$data = $this->parse((string) $response->getBody()); |
45
|
|
|
|
46
|
|
|
return $this->buildResponse($data); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $data |
51
|
|
|
* |
52
|
|
|
* @return mixed[] |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
private function parse($data) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if ($this->getFormat() === self::FORMAT_JSON) { |
57
|
|
|
return json_decode($data, true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->getXmlParser()->parse($data); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed[] $data |
65
|
|
|
* |
66
|
|
|
* @return ElevationResponse |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
private function buildResponse(array $data) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$response = new ElevationResponse(); |
71
|
|
|
$response->setStatus($data['status']); |
72
|
|
|
$response->setResults($this->buildResults($data['results'])); |
73
|
|
|
|
74
|
|
|
return $response; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed[] $data |
79
|
|
|
* |
80
|
|
|
* @return ElevationResult[] |
81
|
|
|
*/ |
82
|
|
|
private function buildResults(array $data) |
83
|
|
|
{ |
84
|
|
|
$results = []; |
85
|
|
|
|
86
|
|
|
foreach ($data as $result) { |
87
|
|
|
$results[] = $this->buildResult($result); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $results; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed[] $data |
95
|
|
|
* |
96
|
|
|
* @return ElevationResult |
97
|
|
|
*/ |
98
|
|
|
private function buildResult(array $data) |
99
|
|
|
{ |
100
|
|
|
$element = new ElevationResult(); |
101
|
|
|
$element->setLocation($this->buildCoordinate($data['location'])); |
102
|
|
|
$element->setElevation($data['elevation']); |
103
|
|
|
|
104
|
|
|
if (isset($data['resolution'])) { |
105
|
|
|
$element->setResolution($data['resolution']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $element; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param mixed[] $data |
113
|
|
|
* |
114
|
|
|
* @return Coordinate |
115
|
|
|
*/ |
116
|
|
|
private function buildCoordinate(array $data) |
117
|
|
|
{ |
118
|
|
|
return new Coordinate($data['lat'], $data['lng']); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.