Completed
Push — master ( be56de...893344 )
by Steven
07:55
created

src/Drivers/IpInfoPremium.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Stevebauman\Location\Drivers;
4
5
use Illuminate\Support\Fluent;
6
use Stevebauman\Location\Position;
7
8
class IpInfoPremium extends Driver
9
{
10
    public function url()
11
    {
12
        return 'https://ipinfo.io/';
13
    }
14
15
    public function token()
16
    {
17
        return config('location.ipinfo');
18
    }
19
20 View Code Duplication
    protected function hydrate(Position $position, Fluent $location)
0 ignored issues
show
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...
21
    {
22
        $position->countryCode = $location->country;
23
        $position->regionName = $location->region;
24
        $position->cityName = $location->city;
25
        $position->zipCode = $location->postal;
26
27
        if ($location->loc) {
28
            $coords = explode(',', $location->loc);
29
30
            if (array_key_exists(0, $coords)) {
31
                $position->latitude = $coords[0];
32
            }
33
34
            if (array_key_exists(1, $coords)) {
35
                $position->longitude = $coords[1];
36
            }
37
        }
38
39
        return $position;
40
    }
41
42
    protected function process($ip)
43
    {
44
        try {
45
            $response = json_decode(file_get_contents($this->url().$ip.'?token='.$this->token()), true);
46
47
            return new Fluent($response);
48
        } catch (\Exception $e) {
49
            return false;
50
        }
51
    }
52
}
53