Completed
Push — master ( a4831f...a4416a )
by ARCANEDEV
07:50
created

GeoIpTracker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

2 Methods

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