Completed
Push — master ( 0f3f6d...5694f4 )
by ARCANEDEV
14s
created

GeoIpTracker::track()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2.0116
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\Support\BindingManager;
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
    /**
22
     * Get the model.
23
     *
24
     * @return \Arcanedev\LaravelTracker\Models\GeoIp
25
     */
26 9
    protected function getModel()
27
    {
28 9
        return $this->makeModel(BindingManager::MODEL_GEOIP);
29
    }
30
31
    /**
32
     * @return \Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector
33
     */
34 9
    private function getGeoIpDetector()
35
    {
36 9
        return $this->make(GeoIpDetector::class);
37
    }
38
39
    /* -----------------------------------------------------------------
40
     |  Main Methods
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Track the ip address.
46
     *
47
     * @param  string  $ipAddress
48
     *
49
     * @return int|null
50
     */
51 9
    public function track($ipAddress)
52
    {
53 9
        if ($data = $this->getGeoIpDetector()->search($ipAddress)) {
54 9
            return $this->getModel()
55 9
                ->newQuery()
56 9
                ->firstOrCreate(Arr::only($data, ['latitude', 'longitude']), $data)
57 9
                ->getKey();
58
        }
59
60
        return null;
61
    }
62
}
63