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 ( a97d0f...9e3fc4 )
by Tobias
07:09 queued 14s
created

FreeGeoIp::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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\FreeGeoIp;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\UnsupportedOperation;
17
use Geocoder\Model\AddressBuilder;
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
use Http\Client\HttpClient;
24
25
/**
26
 * @author William Durand <[email protected]>
27
 */
28
final class FreeGeoIp extends AbstractHttpProvider implements Provider
29
{
30
    /**
31
     * @var string
32
     */
33
    private $baseUrl;
34
35
    /**
36
     * @param HttpClient $client
37
     * @param string     $baseUrl
38
     */
39
    public function __construct(HttpClient $client, string $baseUrl = 'https://freegeoip.net/json/%s')
40
    {
41
        parent::__construct($client);
42
43
        $this->baseUrl = $baseUrl;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function geocodeQuery(GeocodeQuery $query): Collection
50
    {
51
        $address = $query->getText();
52
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
53
            throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.');
54
        }
55
56
        if (in_array($address, ['127.0.0.1', '::1'])) {
57
            return new AddressCollection([$this->getLocationForLocalhost()]);
58
        }
59
60
        $content = $this->getUrlContents(sprintf($this->baseUrl, $address));
61
        $data = json_decode($content, true);
62
63
        // Return empty collection if address was not found
64
        if ('' === $data['region_name']
65
        && '' === $data['region_code']
66
        && 0 === $data['latitude']
67
        && 0 === $data['longitude']
68
        && '' === $data['city']
69
        && '' === $data['zip_code']
70
        && '' === $data['country_name']
71
        && '' === $data['country_code']
72
        && '' === $data['time_zone']) {
73
            return new AddressCollection([]);
74
        }
75
76
        $builder = new AddressBuilder($this->getName());
77
78
        if (!empty($data['region_name'])) {
79
            $builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null);
80
        }
81
82
        if (0 !== $data['latitude'] || 0 !== $data['longitude']) {
83
            $builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null);
84
        }
85
        $builder->setLocality(empty($data['city']) ? null : $data['city']);
86
        $builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']);
87
        $builder->setCountry(empty($data['country_name']) ? null : $data['country_name']);
88
        $builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']);
89
        $builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']);
90
91
        return new AddressCollection([$builder->build()]);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function reverseQuery(ReverseQuery $query): Collection
98
    {
99
        throw new UnsupportedOperation('The FreeGeoIp provider is not able to do reverse geocoding.');
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getName(): string
106
    {
107
        return 'free_geo_ip';
108
    }
109
}
110