Completed
Push — master ( 243903...9a7d92 )
by ARCANEDEV
9s
created

GeoIpTracker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 45
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A getGeoIpDetector() 0 4 1
A track() 0 9 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\AbstractModel;
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 extends AbstractTracker implements GeoIpTrackerContract
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Getters and Setters
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * Get the model.
22
     *
23
     * @return \Arcanedev\LaravelTracker\Models\GeoIp
24
     */
25 18
    protected function getModel()
26
    {
27 18
        return $this->makeModel(AbstractModel::MODEL_GEOIP);
28
    }
29
30
    /**
31
     * @return \Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector
32
     */
33 18
    private function getGeoIpDetector()
34
    {
35 18
        return $this->make(GeoIpDetector::class);
36
    }
37
38
    /* ------------------------------------------------------------------------------------------------
39
     |  Main Functions
40
     | ------------------------------------------------------------------------------------------------
41
     */
42
    /**
43
     * Track the ip address.
44
     *
45
     * @param  string  $ipAddress
46
     *
47
     * @return int|null
48
     */
49 18
    public function track($ipAddress)
50
    {
51 18
        if ($data = $this->getGeoIpDetector()->search($ipAddress)) {
52 18
            return $this->getModel()
53 18
                        ->firstOrCreate(Arr::only($data, ['latitude', 'longitude']), $data)->id;
54
        }
55
56
        return null;
57
    }
58
}
59