IpApi::process()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 3
nop 1
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 IpApi extends Driver
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected function url($ip)
15
    {
16
        return "http://ip-api.com/json/$ip";
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function hydrate(Position $position, Fluent $location)
23
    {
24
        $position->countryName = $location->country;
25
        $position->countryCode = $location->countryCode;
26
        $position->regionCode = $location->region;
27
        $position->regionName = $location->regionName;
28
        $position->cityName = $location->city;
29
        $position->zipCode = $location->zip;
30
        $position->latitude = (string) $location->lat;
31
        $position->longitude = (string) $location->lon;
32
        $position->areaCode = $location->region;
33
34
        return $position;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function process($ip)
41
    {
42
        try {
43
            $response = json_decode($this->getUrlContent($this->url($ip)), true);
44
45
            return new Fluent($response);
46
        } catch (Exception $e) {
47
            return false;
48
        }
49
    }
50
}
51