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

MaxmindApiDriver   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 31.82%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 55
ccs 7
cts 22
cp 0.3182
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 1
A locate() 0 18 1
1
<?php namespace Arcanedev\GeoIP\Drivers;
2
3
use GeoIp2\WebService\Client;
4
5
/**
6
 * Class     MaxmindApiDriver
7
 *
8
 * @package  Arcanedev\GeoIP\Drivers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class MaxmindApiDriver extends AbstractDriver
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Http client instance.
19
     *
20
     * @var \GeoIp2\WebService\Client
21
     */
22
    protected $client;
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Main Functions
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Init the driver.
30
     */
31 6
    protected function init()
32
    {
33 6
        $this->client = new Client(
34 6
            $this->getOption('user_id'),
35 6
            $this->getOption('license_key'),
36 6
            $this->getOption('locales', ['en'])
37 3
        );
38 6
    }
39
40
    /**
41
     * Locate the ip address.
42
     *
43
     * @param  string  $ipAddress
44
     *
45
     * @return \Arcanedev\GeoIP\Location
46
     */
47
    public function locate($ipAddress)
48
    {
49
        $record = $this->client->city($ipAddress);
50
51
        return $this->hydrate([
52
            'ip'          => $ipAddress,
53
            'iso_code'    => $record->country->isoCode,
54
            'country'     => $record->country->name,
55
            'city'        => $record->city->name,
56
            'state'       => $record->mostSpecificSubdivision->name,
57
            'state_code'  => $record->mostSpecificSubdivision->isoCode,
58
            'postal_code' => $record->postal->code,
59
            'latitude'    => $record->location->latitude,
60
            'longitude'   => $record->location->longitude,
61
            'timezone'    => $record->location->timeZone,
62
            'continent'   => $record->continent->code,
63
        ]);
64
    }
65
}
66