Completed
Push — master ( a41bbe...18bc86 )
by ARCANEDEV
08:47
created

GeoIpDetector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 81
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getGeoliteFileName() 0 4 1
A search() 0 13 3
A renderData() 0 17 1
1
<?php namespace Arcanedev\LaravelTracker\Detectors;
2
3
use Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector as GeoIpDetectorContract;
4
use GeoIp2\Database\Reader as GeoIpReader;
5
use GeoIp2\Exception\AddressNotFoundException;
6
7
/**
8
 * Class     GeoIpDetector
9
 *
10
 * @package  Arcanedev\LaravelTracker\Detectors
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class GeoIpDetector implements GeoIpDetectorContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    private $reader;
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Constructor
23
     | ------------------------------------------------------------------------------------------------
24
     */
25 36
    public function __construct()
26
    {
27 36
        $this->reader = new GeoIpReader($this->getGeoliteFileName());
28 36
    }
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Main Functions
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    /**
35
     * Get the geoip data.
36
     *
37
     * @param  string  $ipAddress
38
     *
39
     * @return array|null
40
     */
41 12
    public function search($ipAddress)
42
    {
43
        try {
44 12
            if ($cityModel = $this->reader->city($ipAddress)) {
45 6
                return $this->renderData($cityModel);
46
            }
47
        }
48 6
        catch (AddressNotFoundException $e) {
49
            // do nothing
50
        }
51
52 9
        return null;
53
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Other Functions
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Render the data.
61
     *
62
     * @param  \GeoIp2\Model\City  $cityModel
63
     *
64
     * @return array
65
     */
66 6
    private function renderData($cityModel)
67
    {
68
        return [
69 6
            'latitude'       => $cityModel->location->latitude,
70 6
            'longitude'      => $cityModel->location->longitude,
71 6
            'country_code'   => $cityModel->country->isoCode,
72 3
            'country_code3'  => null,
73 6
            'country_name'   => $cityModel->country->name,
74 6
            'region'         => $cityModel->continent->code,
75 6
            'city'           => $cityModel->city->name,
76 6
            'postal_code'    => $cityModel->postal->code,
77 3
            'area_code'      => null,
78 3
            'dma_code'       => null,
79 6
            'metro_code'     => $cityModel->location->metroCode,
80 6
            'continent_code' => $cityModel->continent->code,
81 3
        ];
82
    }
83
84
    /**
85
     * Get the GeoLiteFileName.
86
     *
87
     * @return string
88
     */
89 36
    private function getGeoliteFileName()
90
    {
91 36
        return __DIR__ . '/../../data/GeoLite2-City.mmdb';
92
    }
93
}
94