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.

BingMaps::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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\BingMaps;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\InvalidCredentials;
17
use Geocoder\Exception\UnsupportedOperation;
18
use Geocoder\Model\AddressBuilder;
19
use Geocoder\Model\AddressCollection;
20
use Geocoder\Query\GeocodeQuery;
21
use Geocoder\Query\ReverseQuery;
22
use Geocoder\Http\Provider\AbstractHttpProvider;
23
use Geocoder\Provider\Provider;
24
use Http\Client\HttpClient;
25
26
/**
27
 * @author David Guyon <[email protected]>
28
 */
29
final class BingMaps extends AbstractHttpProvider implements Provider
30
{
31
    /**
32
     * @var string
33
     */
34
    const GEOCODE_ENDPOINT_URL = 'https://dev.virtualearth.net/REST/v1/Locations/?maxResults=%d&q=%s&key=%s&incl=ciso2';
35
36
    /**
37
     * @var string
38
     */
39
    const REVERSE_ENDPOINT_URL = 'https://dev.virtualearth.net/REST/v1/Locations/%F,%F?key=%s&incl=ciso2';
40
41
    /**
42
     * @var string
43
     */
44
    private $apiKey;
45
46
    /**
47
     * @param HttpClient $client An HTTP adapter
48
     * @param string     $apiKey An API key
49
     */
50
    public function __construct(HttpClient $client, string $apiKey)
51
    {
52
        if (empty($apiKey)) {
53
            throw new InvalidCredentials('No API key provided.');
54
        }
55
56
        $this->apiKey = $apiKey;
57
        parent::__construct($client);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function geocodeQuery(GeocodeQuery $query): Collection
64
    {
65
        // This API doesn't handle IPs
66
        if (filter_var($query->getText(), FILTER_VALIDATE_IP)) {
67
            throw new UnsupportedOperation('The BingMaps provider does not support IP addresses, only street addresses.');
68
        }
69
70
        $url = sprintf(self::GEOCODE_ENDPOINT_URL, $query->getLimit(), urlencode($query->getText()), $this->apiKey);
71
72
        return $this->executeQuery($url, $query->getLocale(), $query->getLimit());
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function reverseQuery(ReverseQuery $query): Collection
79
    {
80
        $coordinates = $query->getCoordinates();
81
        $url = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates->getLatitude(), $coordinates->getLongitude(), $this->apiKey);
82
83
        return $this->executeQuery($url, $query->getLocale(), $query->getLimit());
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getName(): string
90
    {
91
        return 'bing_maps';
92
    }
93
94
    /**
95
     * @param string $url
96
     * @param string $locale
0 ignored issues
show
Documentation introduced by
Should the type for parameter $locale not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
97
     * @param int    $limit
98
     *
99
     * @return \Geocoder\Collection
100
     */
101
    private function executeQuery(string $url, string $locale = null, int $limit): Collection
102
    {
103
        if (null !== $locale) {
104
            $url = sprintf('%s&culture=%s', $url, str_replace('_', '-', $locale));
105
        }
106
107
        $content = $this->getUrlContents($url);
108
        $json = json_decode($content);
109
110
        if (!isset($json->resourceSets[0]) || !isset($json->resourceSets[0]->resources)) {
111
            return new AddressCollection([]);
112
        }
113
114
        $data = (array) $json->resourceSets[0]->resources;
115
116
        $results = [];
117
        foreach ($data as $item) {
118
            $builder = new AddressBuilder($this->getName());
119
            $coordinates = (array) $item->geocodePoints[0]->coordinates;
120
            $builder->setCoordinates($coordinates[0], $coordinates[1]);
121
122
            if (isset($item->bbox) && is_array($item->bbox) && count($item->bbox) > 0) {
123
                $builder->setBounds($item->bbox[0], $item->bbox[1], $item->bbox[2], $item->bbox[3]);
124
            }
125
126
            $builder->setStreetName($item->address->addressLine ?? null);
127
            $builder->setPostalCode($item->address->postalCode ?? null);
128
            $builder->setLocality($item->address->locality ?? null);
129
            $builder->setCountry($item->address->countryRegion ?? null);
130
            $builder->setCountryCode($item->address->countryRegionIso2 ?? null);
131
132
            foreach (['adminDistrict', 'adminDistrict2'] as $i => $property) {
133
                if (property_exists($item->address, $property)) {
134
                    $builder->addAdminLevel($i + 1, $item->address->{$property}, null);
135
                }
136
            }
137
138
            $results[] = $builder->build();
139
140
            if (count($results) >= $limit) {
141
                break;
142
            }
143
        }
144
145
        return new AddressCollection($results);
146
    }
147
}
148