Completed
Push — master ( 1d4f14...5b6fa1 )
by Steven
12:16
created

IpInfo::hydrate()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
rs 8.7972
c 1
b 0
f 0
cc 4
eloc 13
nc 5
nop 2
1
<?php
2
3
namespace Stevebauman\Location\Drivers;
4
5
use Illuminate\Support\Fluent;
6
use Stevebauman\Location\Position;
7
8
class IpInfo extends Driver
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    protected function url()
14
    {
15
        return 'http://ipinfo.io/';
16
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected function hydrate(Position $position, Fluent $location)
22
    {
23
        $position->driver = IpInfo::class;
24
25
        $position->countryCode = $location->country;
26
        $position->regionName = $location->region;
27
        $position->cityName = $location->city;
28
        $position->zipCode = $location->postal;
29
30
        if ($location->loc) {
31
            $coords = explode(',', $location->loc);
32
33
            if (array_key_exists(0, $coords)) {
34
                $position->latitude = $coords[0];
35
            }
36
37
            if (array_key_exists(1, $coords)) {
38
                $position->longitude = $coords[1];
39
            }
40
        }
41
42
        return $position;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 View Code Duplication
    protected function process($ip)
0 ignored issues
show
Duplication introduced by Steve Bauman
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        try {
51
            $response = json_decode(file_get_contents($this->url().$ip));
52
53
            return new Fluent($response);
54
        } catch (\Exception $e) {
55
            return false;
56
        }
57
    }
58
}
59