MaxmindDatabaseDriver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.19%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 129
ccs 33
cts 37
cp 0.8919
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDatabasePath() 0 4 1
A getLocales() 0 4 1
A isDatabaseExists() 0 4 1
A init() 0 9 1
A locate() 0 18 1
A update() 0 7 1
A checkDatabaseFile() 0 12 3
1
<?php namespace Arcanedev\GeoIP\Drivers;
2
3
use Arcanedev\GeoIP\Tasks\DownloadMaxmindDatabaseTask;
4
use GeoIp2\Database\Reader;
5
6
/**
7
 * Class     MaxmindDatabaseDriver
8
 *
9
 * @package  Arcanedev\GeoIP\Drivers
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class MaxmindDatabaseDriver extends AbstractDriver
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Driver reader instance.
21
     *
22
     * @var \GeoIp2\Database\Reader
23
     */
24
    protected $reader;
25
26
    /* -----------------------------------------------------------------
27
     |  Getters & Setters
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * Get the database path.
33
     *
34
     * @return string|null
35
     */
36 12
    private function getDatabasePath()
37
    {
38 12
        return $this->getOption('database-path');
39
    }
40
41
    /**
42
     * Get the locales.
43
     *
44
     * @return array
45
     */
46 12
    private function getLocales()
47
    {
48 12
        return $this->getOption('locales', ['en']);
49
    }
50
51
    /* -----------------------------------------------------------------
52
     |  Main Methods
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Init the driver.
58
     */
59 12
    protected function init()
60
    {
61 12
        $this->checkDatabaseFile();
62
63 12
        $this->reader = new Reader(
64 12
            $this->getDatabasePath(),
65 12
            $this->getLocales()
66
        );
67 12
    }
68
69
    /**
70
     * Locate the ip address.
71
     *
72
     * @param  string  $ipAddress
73
     *
74
     * @return \Arcanedev\GeoIP\Location
75
     */
76 6
    public function locate($ipAddress)
77
    {
78 6
        $record = $this->reader->city($ipAddress);
79
80 3
        return $this->hydrate([
81 3
            'ip'          => $ipAddress,
82 3
            'iso_code'    => $record->country->isoCode,
83 3
            'country'     => $record->country->name,
84 3
            'city'        => $record->city->name,
85 3
            'state'       => $record->mostSpecificSubdivision->name,
86 3
            'state_code'  => $record->mostSpecificSubdivision->isoCode,
87 3
            'postal_code' => $record->postal->code,
88 3
            'latitude'    => $record->location->latitude,
89 3
            'longitude'   => $record->location->longitude,
90 3
            'timezone'    => $record->location->timeZone,
91 3
            'continent'   => $record->continent->code,
92
        ]);
93
    }
94
95
    /**
96
     * Update function for service.
97
     *
98
     * @return bool
99
     *
100
     * @throws \Exception
101
     */
102
    public function update()
103
    {
104
        return DownloadMaxmindDatabaseTask::run(
105
            $this->getOption('update-url'),
106
            $this->getDatabasePath()
107
        );
108
    }
109
110
    /* -----------------------------------------------------------------
111
     |  Other Methods
112
     | -----------------------------------------------------------------
113
     */
114
115
    /**
116
     * Check if the database exists.
117
     *
118
     * @return bool
119
     */
120 12
    private function isDatabaseExists()
121
    {
122 12
        return file_exists($this->getDatabasePath());
123
    }
124
125
    /**
126
     * Check if database file exists, otherwise copy the dummy one.
127
     */
128 12
    private function checkDatabaseFile()
129
    {
130 12
        if ($this->isDatabaseExists()) return;
131
132 3
        $pathinfo = pathinfo($path = $this->getDatabasePath());
133
134 3
        if ( ! file_exists($pathinfo['dirname'])) {
135 3
            mkdir($pathinfo['dirname'], 0777, true);
136
        }
137
138 3
        copy(realpath(__DIR__ . '/../../data/geoip.mmdb'), $path);
139 3
    }
140
}
141