Completed
Push — master ( a55832...6732db )
by Tobias
02:19
created

FreeGeoIp::geocodeQuery()   D

Complexity

Conditions 21
Paths 12

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 21

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 32
cts 32
cp 1
rs 4.1666
c 0
b 0
f 0
cc 21
nc 12
nop 1
crap 21

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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|null
32
     */
33
    private $baseUrl;
34
35
    /**
36
     * @param HttpClient $client
37
     * @param string     $baseUrl
38
     */
39 27
    public function __construct(HttpClient $client, string $baseUrl = 'https://freegeoip.app/json/%s')
40
    {
41 27
        parent::__construct($client);
42
43 27
        $this->baseUrl = $baseUrl;
44 27
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 25
    public function geocodeQuery(GeocodeQuery $query): Collection
50
    {
51 25
        $address = $query->getText();
52
53 25
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
54 1
            throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.');
55
        }
56
57 24
        if (in_array($address, ['127.0.0.1', '::1'])) {
58 2
            return new AddressCollection([$this->getLocationForLocalhost()]);
59
        }
60
61 22
        $request = $this->getRequest(sprintf($this->baseUrl, $address));
62
63 22
        if (null !== $query->getLocale()) {
64 5
            $request = $request->withHeader('Accept-Language', $query->getLocale());
65
        }
66
67 22
        $body = $this->getParsedResponse($request);
68 11
        $data = json_decode($body, true);
69
70
        // Return empty collection if address was not found
71 11
        if ('' === $data['region_name']
72 11
        && '' === $data['region_code']
73 11
        && 0 === $data['latitude']
74 11
        && 0 === $data['longitude']
75 11
        && '' === $data['city']
76 11
        && '' === $data['zip_code']
77 11
        && '' === $data['country_name']
78 11
        && '' === $data['country_code']
79 11
        && '' === $data['time_zone']) {
80 1
            return new AddressCollection([]);
81
        }
82
83 10
        $builder = new AddressBuilder($this->getName());
84
85 10
        if (!empty($data['region_name'])) {
86 7
            $builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null);
87
        }
88
89 10
        if (0 !== $data['latitude'] || 0 !== $data['longitude']) {
90 10
            $builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null);
91
        }
92 10
        $builder->setLocality(empty($data['city']) ? null : $data['city']);
93 10
        $builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']);
94 10
        $builder->setCountry(empty($data['country_name']) ? null : $data['country_name']);
95 10
        $builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']);
96 10
        $builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']);
97
98 10
        return new AddressCollection([$builder->build()]);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function reverseQuery(ReverseQuery $query): Collection
105
    {
106 1
        throw new UnsupportedOperation('The FreeGeoIp provider is not able to do reverse geocoding.');
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 13
    public function getName(): string
113
    {
114 13
        return 'free_geo_ip';
115
    }
116
}
117