GeoIpDetector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 82
ccs 18
cts 20
cp 0.9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A search() 0 13 3
A transform() 0 16 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
    /**
20
     * @var \Arcanedev\GeoIP\Contracts\GeoIP
21
     */
22
    private $geoIp;
23
24
    /* -----------------------------------------------------------------
25
     |  Constructor
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * GeoIpDetector constructor.
31
     *
32
     * @param  \Arcanedev\GeoIP\Contracts\GeoIP  $geoIp
33
     */
34 9
    public function __construct(GeoIP $geoIp)
35
    {
36 9
        $this->geoIp = $geoIp;
37 9
    }
38
39
    /* -----------------------------------------------------------------
40
     |  Main Methods
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Get the geoip data.
46
     *
47
     * @param  string  $ipAddress
48
     *
49
     * @return array|null
50
     */
51 9
    public function search($ipAddress)
52
    {
53
        try {
54 9
            if ($location = $this->geoIp->location($ipAddress)) {
55 9
                return $this->transform($location);
56
            }
57
        }
58
        catch (\Exception $e) {
59
            // do nothing
60
        }
61
62
        return null;
63
    }
64
65
    /* -----------------------------------------------------------------
66
     |  Other Methods
67
     | -----------------------------------------------------------------
68
     */
69
70
    /**
71
     * Render the data.
72
     *
73
     * @param  \Arcanedev\GeoIP\Location  $location
74
     *
75
     * @return array
76
     */
77 9
    private function transform($location)
78
    {
79
        return [
80 9
            'iso_code'    => $location->iso_code,
81 9
            'country'     => $location->country,
82 9
            'city'        => $location->city,
83 9
            'state'       => $location->state,
84 9
            'state_code'  => $location->state_code,
85 9
            'postal_code' => $location->postal_code,
86 9
            'latitude'    => $location->latitude,
87 9
            'longitude'   => $location->longitude,
88 9
            'timezone'    => $location->timezone,
89 9
            'continent'   => $location->continent,
90 9
            'currency'    => $location->currency,
91
        ];
92
    }
93
}
94