Completed
Push — master ( 80b7de...7dea50 )
by Steven
01:57
created

IpInfo::hydrate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.0534
c 1
b 0
f 0
cc 4
eloc 12
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->countryCode = $location->country;
24
        $position->regionName = $location->region;
25
        $position->cityName = $location->city;
26
        $position->zipCode = $location->postal;
27
28
        if ($location->loc) {
29
            $coords = explode(',', $location->loc);
30
31
            if (array_key_exists(0, $coords)) {
32
                $position->latitude = $coords[0];
33
            }
34
35
            if (array_key_exists(1, $coords)) {
36
                $position->longitude = $coords[1];
37
            }
38
        }
39
40
        return $position;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 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...
47
    {
48
        try {
49
            $response = json_decode(file_get_contents($this->url().$ip));
50
51
            return new Fluent($response);
52
        } catch (\Exception $e) {
53
            return false;
54
        }
55
    }
56
}
57