Completed
Push — master ( 6c5932...f55ac0 )
by ARCANEDEV
07:13
created

FreeGeoIpDriver   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 67
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 1
A locate() 0 20 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 30
                'User-Agent' => 'Laravel-GeoIP',
45
            ]
46 30
        ]);
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 15
            '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 6
        ]);
77
    }
78
}
79