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

GeoIpTracker::getModel()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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