Completed
Push — master ( f76772...1672bb )
by ARCANEDEV
07:47
created

GeoIpTracker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 45
ccs 6
cts 8
cp 0.75
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A track() 0 10 2
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector;
4
use Arcanedev\LaravelTracker\Contracts\Trackers\GeoIpTracker as GeoIpTrackerContract;
5
use Arcanedev\LaravelTracker\Models\GeoIp;
6
use Illuminate\Support\Arr;
7
8
/**
9
 * Class     GeoIpTracker
10
 *
11
 * @package  Arcanedev\LaravelTracker\Trackers
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class GeoIpTracker implements GeoIpTrackerContract
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /** @var \Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector */
21
    private $geoIpDetector;
22
23
    /* ------------------------------------------------------------------------------------------------
24
     |  Constructor
25
     | ------------------------------------------------------------------------------------------------
26
     */
27
    /**
28
     * GeoIpTracker constructor.
29
     *
30
     * @param  \Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector  $geoIpDetector
31
     */
32 24
    public function __construct(GeoIpDetector $geoIpDetector)
33
    {
34 24
        $this->geoIpDetector = $geoIpDetector;
35 24
    }
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Main Functions
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * Track the ip address.
43
     *
44
     * @param  string  $ipAddress
45
     *
46
     * @return int|null
47
     */
48 6
    public function track($ipAddress)
49
    {
50 6
        if ($data = $this->geoIpDetector->search($ipAddress)) {
51
            $model = GeoIp::firstOrCreate(Arr::only($data, ['latitude', 'longitude']), $data);
52
53
            return $model->id;
54
        }
55
56 6
        return null;
57
    }
58
}
59