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.

MapQuest::executeQuery()   D
last analyzed

Complexity

Conditions 18
Paths 14

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 4.9067
c 0
b 0
f 0
cc 18
eloc 29
nc 14
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\MapQuest;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\InvalidCredentials;
17
use Geocoder\Exception\UnsupportedOperation;
18
use Geocoder\Http\Provider\AbstractHttpProvider;
19
use Geocoder\Model\Address;
20
use Geocoder\Model\AddressCollection;
21
use Geocoder\Query\GeocodeQuery;
22
use Geocoder\Query\ReverseQuery;
23
use Geocoder\Provider\Provider;
24
use Http\Client\HttpClient;
25
26
/**
27
 * @author William Durand <[email protected]>
28
 */
29
final class MapQuest extends AbstractHttpProvider implements Provider
30
{
31
    /**
32
     * @var string
33
     */
34
    const OPEN_GEOCODE_ENDPOINT_URL = 'https://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s&thumbMaps=false';
35
36
    /**
37
     * @var string
38
     */
39
    const OPEN_REVERSE_ENDPOINT_URL = 'https://open.mapquestapi.com/geocoding/v1/reverse?key=%s&lat=%F&lng=%F';
40
41
    /**
42
     * @var string
43
     */
44
    const LICENSED_GEOCODE_ENDPOINT_URL = 'https://www.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s&thumbMaps=false';
45
46
    /**
47
     * @var string
48
     */
49
    const LICENSED_REVERSE_ENDPOINT_URL = 'https://www.mapquestapi.com/geocoding/v1/reverse?key=%s&lat=%F&lng=%F';
50
51
    /**
52
     * MapQuest offers two geocoding endpoints one commercial (true) and one open (false)
53
     * More information: http://developer.mapquest.com/web/tools/getting-started/platform/licensed-vs-open.
54
     *
55
     * @var bool
56
     */
57
    private $licensed;
58
59
    /**
60
     * @var string
61
     */
62
    private $apiKey;
63
64
    /**
65
     * @param HttpClient $client   an HTTP adapter
66
     * @param string     $apiKey   an API key
67
     * @param bool       $licensed true to use MapQuest's licensed endpoints, default is false to use the open endpoints (optional)
68
     */
69
    public function __construct(HttpClient $client, string $apiKey, bool $licensed = false)
70
    {
71
        if (empty($apiKey)) {
72
            throw new InvalidCredentials('No API key provided.');
73
        }
74
75
        $this->apiKey = $apiKey;
76
        $this->licensed = $licensed;
77
        parent::__construct($client);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function geocodeQuery(GeocodeQuery $query): Collection
84
    {
85
        $address = $query->getText();
86
87
        // This API doesn't handle IPs
88
        if (filter_var($address, FILTER_VALIDATE_IP)) {
89
            throw new UnsupportedOperation('The MapQuest provider does not support IP addresses, only street addresses.');
90
        }
91
92
        if ($this->licensed) {
93
            $url = sprintf(self::LICENSED_GEOCODE_ENDPOINT_URL, urlencode($address), $query->getLimit(), $this->apiKey);
94
        } else {
95
            $url = sprintf(self::OPEN_GEOCODE_ENDPOINT_URL, urlencode($address), $query->getLimit(), $this->apiKey);
96
        }
97
98
        return $this->executeQuery($url);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function reverseQuery(ReverseQuery $query): Collection
105
    {
106
        $coordinates = $query->getCoordinates();
107
        $longitude = $coordinates->getLongitude();
108
        $latitude = $coordinates->getLatitude();
109
110
        if ($this->licensed) {
111
            $url = sprintf(self::LICENSED_REVERSE_ENDPOINT_URL, $this->apiKey, $latitude, $longitude);
112
        } else {
113
            $url = sprintf(self::OPEN_REVERSE_ENDPOINT_URL, $this->apiKey, $latitude, $longitude);
114
        }
115
116
        return $this->executeQuery($url);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getName(): string
123
    {
124
        return 'map_quest';
125
    }
126
127
    /**
128
     * @param string $url
129
     *
130
     * @return AddressCollection
131
     */
132
    private function executeQuery(string $url): AddressCollection
133
    {
134
        $content = $this->getUrlContents($url);
135
        $json = json_decode($content, true);
136
137
        if (!isset($json['results']) || empty($json['results'])) {
138
            return new AddressCollection([]);
139
        }
140
141
        $locations = $json['results'][0]['locations'];
142
143
        if (empty($locations)) {
144
            return new AddressCollection([]);
145
        }
146
147
        $results = [];
148
        foreach ($locations as $location) {
149
            if ($location['street'] || $location['postalCode'] || $location['adminArea5'] || $location['adminArea4'] || $location['adminArea3']) {
150
                $admins = [];
151
152
                if ($location['adminArea3']) {
153
                    $admins[] = ['name' => $location['adminArea3'], 'level' => 1];
154
                }
155
156
                if ($location['adminArea4']) {
157
                    $admins[] = ['name' => $location['adminArea4'], 'level' => 2];
158
                }
159
160
                $results[] = Address::createFromArray([
161
                    'providedBy' => $this->getName(),
162
                    'latitude' => $location['latLng']['lat'],
163
                    'longitude' => $location['latLng']['lng'],
164
                    'streetName' => $location['street'] ?: null,
165
                    'locality' => $location['adminArea5'] ?: null,
166
                    'postalCode' => $location['postalCode'] ?: null,
167
                    'adminLevels' => $admins,
168
                    'country' => $location['adminArea1'] ?: null,
169
                    'countryCode' => $location['adminArea1'] ?: null,
170
                ]);
171
            }
172
        }
173
174
        if (empty($results)) {
175
            return new AddressCollection([]);
176
        }
177
178
        return new AddressCollection($results);
179
    }
180
}
181