Completed
Push — master ( d0dd0d...df62c5 )
by ARCANEDEV
12s
created

src/Drivers/FreeGeoIpDriver.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 namespace Arcanedev\GeoIP\Drivers;
2
3
use Arcanedev\GeoIP\Tasks\DownloadContinentsFileTask;
4
use GuzzleHttp\Client;
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class     FreeGeoIpDriver
9
 *
10
 * @package  Arcanedev\GeoIP\Drivers
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class FreeGeoIpDriver extends AbstractDriver
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Http client instance.
21
     *
22
     * @var \GuzzleHttp\Client
23
     */
24
    protected $client;
25
26
    /**
27
     * An array of continents.
28
     *
29
     * @var array
30
     */
31
    protected $continents = [];
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Main Functions
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Init the driver.
39
     */
40 60
    protected function init()
41
    {
42 60
        $this->client = new Client([
43 60
            'base_uri' => 'http://freegeoip.net/',
44
            'headers' => [
45 30
                'User-Agent' => 'Laravel-GeoIP',
46
            ]
47 30
        ]);
48
49
        // Set continents
50 60
        if (file_exists($path = $this->getOption('continents-path'))) {
51
            $this->continents = json_decode(file_get_contents($path), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode(file_get_contents($path), true) of type * is incompatible with the declared type array of property $continents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        }
53 60
    }
54
55
    /**
56
     * Locate the ip address.
57
     *
58
     * @param  string $ipAddress
59
     *
60
     * @return \Arcanedev\GeoIP\Location
61
     */
62 18
    public function locate($ipAddress)
63
    {
64 18
        $response = $this->client->get("json/$ipAddress");
65
66 18
        $data = json_decode($response->getBody());
67
68 18
        return $this->hydrate([
69 18
            'ip'          => $ipAddress,
70 18
            'iso_code'    => $data->country_code,
71 18
            'country'     => $data->country_name,
72 18
            'city'        => $data->city,
73 18
            'state'       => $data->region_name,
74 18
            'state_code'  => $data->region_code,
75 18
            'postal_code' => $data->zip_code,
76 18
            'latitude'    => $data->latitude,
77 18
            'longitude'   => $data->longitude,
78 18
            'timezone'    => $data->time_zone,
79 18
            'continent'   => $this->getContinent($data->country_code),
80 9
        ]);
81
    }
82
83
    /**
84
     * Update function for service.
85
     *
86
     * @return bool
87
     *
88
     * @throws \Exception
89
     */
90
    public function update()
91
    {
92
        return DownloadContinentsFileTask::run(
93
            'http://dev.maxmind.com/static/csv/codes/country_continent.csv',
94
            $this->getOption('continents-path')
95
        );
96
    }
97
98
    /**
99
     * Get continent based on country code.
100
     *
101
     * @param  string  $code
102
     *
103
     * @return string
104
     */
105 18
    private function getContinent($code)
106
    {
107 18
        return Arr::get($this->continents, $code, 'Unknown');
108
    }
109
}
110