IpInfo   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 2
b 0
f 0
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 20 4
A process() 0 8 2
A url() 0 9 2
1
<?php
2
3
namespace Stevebauman\Location\Drivers;
4
5
use Exception;
6
use Illuminate\Support\Fluent;
7
use Stevebauman\Location\Position;
8
9
class IpInfo extends Driver
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected function url($ip)
15
    {
16
        $url = "http://ipinfo.io/$ip";
17
18
        if ($token = config('location.ipinfo.token')) {
19
            $url .= '?token='.$token;
20
        }
21
22
        return $url;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function hydrate(Position $position, Fluent $location)
29
    {
30
        $position->countryCode = $location->country;
31
        $position->regionName = $location->region;
32
        $position->cityName = $location->city;
33
        $position->zipCode = $location->postal;
34
35
        if ($location->loc) {
36
            $coords = explode(',', $location->loc);
37
38
            if (array_key_exists(0, $coords)) {
39
                $position->latitude = $coords[0];
40
            }
41
42
            if (array_key_exists(1, $coords)) {
43
                $position->longitude = $coords[1];
44
            }
45
        }
46
47
        return $position;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function process($ip)
54
    {
55
        try {
56
            $response = json_decode($this->getUrlContent($this->url($ip)));
57
58
            return new Fluent($response);
59
        } catch (Exception $e) {
60
            return false;
61
        }
62
    }
63
}
64