Completed
Push — master ( 83c366...625326 )
by Tobias
05:09 queued 02:13
created

FreeGeoIp::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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