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.

GeoPlugin   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 5
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A geocodeQuery() 0 15 3
A reverseQuery() 0 4 1
A getName() 0 4 1
D executeQuery() 0 48 20
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\GeoPlugin;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\InvalidServerResponse;
17
use Geocoder\Exception\UnsupportedOperation;
18
use Geocoder\Model\Address;
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
25
/**
26
 * @author Andrea Cristaudo <[email protected]>
27
 */
28
final class GeoPlugin extends AbstractHttpProvider implements Provider
29
{
30
    /**
31
     * @var string
32
     */
33
    const GEOCODE_ENDPOINT_URL = 'http://www.geoplugin.net/json.gp?ip=%s';
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function geocodeQuery(GeocodeQuery $query): Collection
39
    {
40
        $address = $query->getText();
41
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
42
            throw new UnsupportedOperation('The GeoPlugin provider does not support street addresses, only IP addresses.');
43
        }
44
45
        if (in_array($address, ['127.0.0.1', '::1'])) {
46
            return new AddressCollection([$this->getLocationForLocalhost()]);
47
        }
48
49
        $url = sprintf(self::GEOCODE_ENDPOINT_URL, $address);
50
51
        return $this->executeQuery($url);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function reverseQuery(ReverseQuery $query): Collection
58
    {
59
        throw new UnsupportedOperation('The GeoPlugin provider is not able to do reverse geocoding.');
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getName(): string
66
    {
67
        return 'geo_plugin';
68
    }
69
70
    /**
71
     * @param string $url
72
     *
73
     * @return AddressCollection
74
     */
75
    private function executeQuery(string $url): AddressCollection
76
    {
77
        $content = $this->getUrlContents($url);
78
        $json = json_decode($content, true);
79
80
        if (!is_array($json) || !count($json)) {
81
            throw InvalidServerResponse::create($url);
82
        }
83
84
        if (!array_key_exists('geoplugin_status', $json) || (200 !== $json['geoplugin_status'] && 206 !== $json['geoplugin_status'])) {
85
            return new AddressCollection([]);
86
        }
87
88
        // Return empty collection if address was not found
89
        if ('' === $json['geoplugin_regionName']
90
        && '' === $json['geoplugin_regionCode']
91
        && '' === $json['geoplugin_city']
92
        && '' === $json['geoplugin_countryName']
93
        && '' === $json['geoplugin_countryCode']
94
        && '0' === $json['geoplugin_latitude']
95
        && '0' === $json['geoplugin_longitude']) {
96
            return new AddressCollection([]);
97
        }
98
99
        $data = array_filter($json);
100
101
        $adminLevels = [];
102
103
        $region = \igorw\get_in($data, ['geoplugin_regionName']);
104
        $regionCode = \igorw\get_in($data, ['geoplugin_regionCode']);
105
106
        if (null !== $region || null !== $regionCode) {
107
            $adminLevels[] = ['name' => $region, 'code' => $regionCode, 'level' => 1];
108
        }
109
110
        $results = [];
111
        $results[] = Address::createFromArray([
112
            'providedBy' => $this->getName(),
113
            'locality' => isset($data['geoplugin_city']) ? $data['geoplugin_city'] : null,
114
            'country' => isset($data['geoplugin_countryName']) ? $data['geoplugin_countryName'] : null,
115
            'countryCode' => isset($data['geoplugin_countryCode']) ? $data['geoplugin_countryCode'] : null,
116
            'adminLevels' => $adminLevels,
117
            'latitude' => isset($data['geoplugin_latitude']) ? $data['geoplugin_latitude'] : null,
118
            'longitude' => isset($data['geoplugin_longitude']) ? $data['geoplugin_longitude'] : null,
119
        ]);
120
121
        return new AddressCollection($results);
122
    }
123
}
124