GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 688c26...edcebb )
by Eric
14:29 queued 11:16
created

Geocoder::buildBound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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\Geocoder;
13
14
use Http\Client\HttpClient;
15
use Http\Message\MessageFactory;
16
use Ivory\GoogleMap\Base\Bound;
17
use Ivory\GoogleMap\Base\Coordinate;
18
use Ivory\GoogleMap\Service\AbstractService;
19
use Ivory\GoogleMap\Service\Geocoder\Request\GeocoderRequestInterface;
20
use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderAddress;
21
use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderGeometry;
22
use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderResponse;
23
use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderResult;
24
25
/**
26
 * @author GeLo <[email protected]>
27
 */
28
class Geocoder extends AbstractService
29
{
30
    /**
31
     * @param HttpClient     $client
32
     * @param MessageFactory $messageFactory
33
     */
34
    public function __construct(HttpClient $client, MessageFactory $messageFactory)
35
    {
36
        parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/geocode');
37
    }
38
39
    /**
40
     * @param GeocoderRequestInterface $request
41
     *
42
     * @return GeocoderResponse
43
     */
44 View Code Duplication
    public function geocode(GeocoderRequestInterface $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
45
    {
46
        $response = $this->getClient()->sendRequest($this->createRequest($request->build()));
47
        $data = $this->parse((string) $response->getBody());
48
49
        return $this->buildResponse($data);
50
    }
51
52
    /**
53
     * @param string $data
54
     *
55
     * @return mixed[]
56
     */
57
    private function parse($data)
58
    {
59
        if ($this->getFormat() === self::FORMAT_JSON) {
60
            return json_decode($data, true);
61
        }
62
63
        return $this->getXmlParser()->parse($data, [
64
            'address_component' => 'address_components',
65
            'type'              => 'types',
66
            'result'            => 'results',
67
        ]);
68
    }
69
70
    /**
71
     * @param mixed[] $data
72
     *
73
     * @return GeocoderResponse
74
     */
75 View Code Duplication
    private function buildResponse(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
76
    {
77
        $response = new GeocoderResponse();
78
        $response->setStatus($data['status']);
79
        $response->setResults($this->buildResults($data['results']));
80
81
        return $response;
82
    }
83
84
    /**
85
     * @param mixed[] $data
86
     *
87
     * @return GeocoderResult[]
88
     */
89
    private function buildResults(array $data)
90
    {
91
        $results = [];
92
93
        foreach ($data as $response) {
94
            $results[] = $this->buildResult($response);
95
        }
96
97
        return $results;
98
    }
99
100
    /**
101
     * @param mixed[] $data
102
     *
103
     * @return GeocoderResult
104
     */
105
    private function buildResult(array $data)
106
    {
107
        $result = new GeocoderResult();
108
        $result->setAddresses($this->buildAddresses($data['address_components']));
109
        $result->setGeometry($this->buildGeometry($data['geometry']));
110
        $result->setPlaceId($data['place_id']);
111
        $result->setFormattedAddress($data['formatted_address']);
112
113
        if (isset($data['types'])) {
114
            $result->setTypes($data['types']);
115
        }
116
117
        if (isset($data['partial_match'])) {
118
            $result->setPartialMatch($data['partial_match']);
119
        }
120
121
        return $result;
122
    }
123
124
    /**
125
     * @param mixed[] $data
126
     *
127
     * @return GeocoderAddress[]
128
     */
129
    private function buildAddresses(array $data)
130
    {
131
        $addresses = [];
132
133
        foreach ($data as $address) {
134
            $addresses[] = $this->buildAddress($address);
135
        }
136
137
        return $addresses;
138
    }
139
140
    /**
141
     * @param mixed[] $data
142
     *
143
     * @return GeocoderAddress
144
     */
145
    private function buildAddress(array $data)
146
    {
147
        $address = new GeocoderAddress();
148
        $address->setLongName($data['long_name']);
149
        $address->setShortName($data['short_name']);
150
        $address->setTypes($data['types']);
151
152
        return $address;
153
    }
154
155
    /**
156
     * @param mixed[] $data
157
     *
158
     * @return GeocoderGeometry
159
     */
160
    private function buildGeometry(array $data)
161
    {
162
        $geometry = new GeocoderGeometry();
163
        $geometry->setLocation($this->buildCoordinate($data['location']));
164
        $geometry->setViewport($this->buildBound($data['viewport']));
165
        $geometry->setLocationType($data['location_type']);
166
167
        if (isset($data['bounds'])) {
168
            $geometry->setBound($this->buildBound($data['bounds']));
169
        }
170
171
        return $geometry;
172
    }
173
174
    /**
175
     * @param mixed[] $data
176
     *
177
     * @return Bound
178
     */
179
    private function buildBound(array $data)
180
    {
181
        return new Bound(
182
            $this->buildCoordinate($data['southwest']),
183
            $this->buildCoordinate($data['northeast'])
184
        );
185
    }
186
187
    /**
188
     * @param mixed[] $data
189
     *
190
     * @return Coordinate
191
     */
192
    private function buildCoordinate(array $data)
193
    {
194
        return new Coordinate($data['lat'], $data['lng']);
195
    }
196
}
197