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

FreeGeoIpDriver::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\GeoIP\Drivers;
2
3
use Arcanedev\GeoIP\Entities\Continents;
4
use GuzzleHttp\Client;
5
6
/**
7
 * Class     FreeGeoIpDriver
8
 *
9
 * @package  Arcanedev\GeoIP\Drivers
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class FreeGeoIpDriver extends AbstractDriver
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * Http client instance.
20
     *
21
     * @var \GuzzleHttp\Client
22
     */
23
    protected $client;
24
25
    /**
26
     * A collection of continents.
27
     *
28
     * @var \Arcanedev\GeoIP\Entities\Continents
29
     */
30
    protected $continents;
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Main Functions
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Init the driver.
38
     */
39 60
    protected function init()
40
    {
41 60
        $this->client = new Client([
42 60
            'base_uri' => 'http://freegeoip.net/',
43
            'headers' => [
44 10
                'User-Agent' => 'Laravel-GeoIP',
45
            ]
46 10
        ]);
47
48 60
        $this->continents = Continents::make();
49 60
    }
50
51
    /**
52
     * Locate the ip address.
53
     *
54
     * @param  string $ipAddress
55
     *
56
     * @return \Arcanedev\GeoIP\Location
57
     */
58 18
    public function locate($ipAddress)
59
    {
60 18
        $response = $this->client->get("json/$ipAddress");
61
62 12
        $data = json_decode($response->getBody());
63
64 12
        return $this->hydrate([
65 13
            'ip'          => $ipAddress,
66 12
            'iso_code'    => $data->country_code,
67 12
            'country'     => $data->country_name,
68 12
            'city'        => $data->city,
69 12
            'state'       => $data->region_name,
70 12
            'state_code'  => $data->region_code,
71 12
            'postal_code' => $data->zip_code,
72 12
            'latitude'    => $data->latitude,
73 12
            'longitude'   => $data->longitude,
74 12
            'timezone'    => $data->time_zone,
75 12
            'continent'   => $this->continents->get($data->country_code, 'Unknown'),
76 2
        ]);
77
    }
78
79
    /**
80
     * Update function for service.
81
     *
82
     * @return bool
83
     *
84
     * @throws \Exception
85
     */
86
    public function update()
87
    {
88
        // Do nothing
89
90
        return true;
91
    }
92
}
93