Completed
Push — master ( f328e9...1657f1 )
by ARCANEDEV
13s
created

GeoIpDetector::renderData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
crap 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 18
    public function __construct()
26
    {
27 18
        $this->reader = new GeoIpReader($this->getGeoliteFileName());
28 18
    }
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 6
        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 18
    private function getGeoliteFileName()
90
    {
91 18
        return __DIR__ . '/../../data/GeoLite2-City.mmdb';
92
    }
93
}
94