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 ( 5e1808...1861ac )
by Tobias
19s
created

IpInfo::geocodeQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
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\IpInfo;
14
15
use Geocoder\Exception\UnsupportedOperation;
16
use Geocoder\Collection;
17
use Geocoder\Model\Address;
18
use Geocoder\Model\AddressCollection;
19
use Geocoder\Query\GeocodeQuery;
20
use Geocoder\Query\ReverseQuery;
21
use Geocoder\Http\Provider\AbstractHttpProvider;
22
use Geocoder\Provider\Provider;
23
24
/**
25
 * @author Roro Neutron <[email protected]>
26
 */
27
final class IpInfo extends AbstractHttpProvider implements Provider
28
{
29
    /**
30
     * @var string
31
     */
32
    const ENDPOINT_URL = 'https://ipinfo.io/%s/json';
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function geocodeQuery(GeocodeQuery $query): Collection
38
    {
39
        $address = $query->getText();
40
41
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
42
            throw new UnsupportedOperation('The IpInfo provider does not support street addresses, only IP addresses.');
43
        }
44
45
        if (in_array($address, ['127.0.0.1', '::1'], true)) {
46
            return new AddressCollection([$this->getLocationForLocalhost()]);
47
        }
48
49
        return $this->executeQuery(sprintf(self::ENDPOINT_URL, $address));
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function reverseQuery(ReverseQuery $query): Collection
56
    {
57
        throw new UnsupportedOperation('The IpInfo provider is not able to do reverse geocoding.');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getName(): string
64
    {
65
        return 'ip_info';
66
    }
67
68
    /**
69
     * @param string $url
70
     *
71
     * @return Collection
72
     */
73
    private function executeQuery(string $url): AddressCollection
74
    {
75
        $content = $this->getUrlContents($url);
76
        $data = json_decode($content, true);
77
78
        if (empty($data) || !isset($data['loc']) || '' === $data['loc']) {
79
            return new AddressCollection([]);
80
        }
81
82
        $location = explode(',', $data['loc']);
83
84
        return new AddressCollection([
85
            Address::createFromArray([
86
                'providedBy' => $this->getName(),
87
                'latitude' => $location[0],
88
                'longitude' => $location[1],
89
                'locality' => $data['city'] ?? null,
90
                'postalCode' => $data['postal'] ?? null,
91
                'adminLevels' => isset($data['region']) ? [['name' => $data['region'], 'level' => 1]] : [],
92
                'countryCode' => $data['country'] ?? null,
93
            ]),
94
        ]);
95
    }
96
}
97