FreeGeoIpDriver   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 69
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 69
ccs 20
cts 20
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
    /**
20
     * Http client instance.
21
     *
22
     * @var \GuzzleHttp\Client
23
     */
24
    protected $client;
25
26
    /**
27
     * A collection of continents.
28
     *
29
     * @var \Arcanedev\GeoIP\Entities\Continents
30
     */
31
    protected $continents;
32
33
    /* -----------------------------------------------------------------
34
     |  Main Methods
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * Init the driver.
40
     */
41 30
    protected function init()
42
    {
43 30
        $this->client = new Client([
44 30
            'base_uri' => 'http://freegeoip.net/',
45
            'headers' => [
46
                'User-Agent' => 'Laravel-GeoIP',
47
            ]
48
        ]);
49
50 30
        $this->continents = Continents::load();
51 30
    }
52
53
    /**
54
     * Locate the ip address.
55
     *
56
     * @param  string $ipAddress
57
     *
58
     * @return \Arcanedev\GeoIP\Location
59
     */
60 9
    public function locate($ipAddress)
61
    {
62 9
        $response = $this->client->get("json/$ipAddress");
63
64 6
        $data = json_decode($response->getBody());
65
66 6
        return $this->hydrate([
67 6
            'ip'          => $ipAddress,
68 6
            'iso_code'    => $data->country_code,
69 6
            'country'     => $data->country_name,
70 6
            'city'        => $data->city,
71 6
            'state'       => $data->region_name,
72 6
            'state_code'  => $data->region_code,
73 6
            'postal_code' => $data->zip_code,
74 6
            'latitude'    => $data->latitude,
75 6
            'longitude'   => $data->longitude,
76 6
            'timezone'    => $data->time_zone,
77 6
            'continent'   => $this->continents->get($data->country_code, 'Unknown'),
78
        ]);
79
    }
80
}
81