Completed
Pull Request — master (#3)
by ARCANEDEV
21:38 queued 04:38
created

GeoIpDetector::getGeoliteFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelTracker\Detectors;
2
3
use Arcanedev\GeoIP\Contracts\GeoIP;
4
use Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector as GeoIpDetectorContract;
5
6
/**
7
 * Class     GeoIpDetector
8
 *
9
 * @package  Arcanedev\LaravelTracker\Detectors
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class GeoIpDetector implements GeoIpDetectorContract
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * @var \Arcanedev\GeoIP\Contracts\GeoIP
20
     */
21
    private $geoip;
22
23
    /* ------------------------------------------------------------------------------------------------
24
     |  Constructor
25 18
     | ------------------------------------------------------------------------------------------------
26
     */
27 18
    public function __construct(GeoIP $geoip)
28 18
    {
29
        $this->geoip = $geoip;
30
    }
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Main Functions
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Get the geoip data.
38
     *
39
     * @param  string  $ipAddress
40
     *
41 18
     * @return array|null
42
     */
43
    public function search($ipAddress)
44 18
    {
45 6
        try {
46
            if ($location = $this->geoip->location($ipAddress)) {
47
                return $this->renderData($location);
48 12
            }
49
        }
50
        catch (\Exception $e) {
51
            // do nothing
52 12
        }
53
54
        return null;
55
    }
56
57
    /* ------------------------------------------------------------------------------------------------
58
     |  Other Functions
59
     | ------------------------------------------------------------------------------------------------
60
     */
61
    /**
62
     * Render the data.
63
     *
64
     * @param  \Arcanedev\GeoIP\Location  $location
65
     *
66 6
     * @return array
67
     */
68
    private function renderData($location)
69 6
    {
70 6
        return [
71 6
            'iso_code'    => $location->iso_code,
72 3
            'country'     => $location->country,
73 6
            'city'        => $location->city,
74 6
            'state'       => $location->state,
75 6
            'state_code'  => $location->state_code,
76 6
            'postal_code' => $location->postal_code,
77 3
            'latitude'    => $location->latitude,
78 3
            'longitude'   => $location->longitude,
79 6
            'timezone'    => $location->timezone,
80 6
            'continent'   => $location->continent,
81 3
            'currency'    => $location->currency,
82
        ];
83
    }
84
}
85