IpApi   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 2
b 0
f 0
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 8 2
A hydrate() 0 13 1
A url() 0 3 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