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

DeviceTracker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 66
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUserAgentParser() 0 4 1
A track() 0 7 1
A getCurrentDeviceProperties() 0 11 2
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Detectors\DeviceDetector;
4
use Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser;
5
use Arcanedev\LaravelTracker\Models\Device;
6
7
/**
8
 * Class     DeviceTracker
9
 *
10
 * @package  Arcanedev\LaravelTracker\Trackers
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class DeviceTracker
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /** @var  \Arcanedev\LaravelTracker\Contracts\Detectors\DeviceDetector */
20
    protected $detector;
21
22
    /* ------------------------------------------------------------------------------------------------
23
     |  Constructor
24
     | ------------------------------------------------------------------------------------------------
25
     */
26 6
    public function __construct()
27
    {
28 6
        $this->detector = app(DeviceDetector::class);
29 6
    }
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Getters & Setters
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * Get the user agent parser.
37
     *
38
     * @return \Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser
39
     */
40 6
    private function getUserAgentParser()
41
    {
42 6
        return app(UserAgentParser::class);
43
    }
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Main Functions
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * Track the device.
51
     *
52
     * @return int
53
     */
54 6
    public function track()
55
    {
56 6
        $data  = $this->getCurrentDeviceProperties();
57 6
        $model = Device::firstOrCreate($data, $data);
58
59
        return $model->id;
60
    }
61
62
    /**
63
     * Get the current device properties.
64
     *
65
     * @return array
66
     */
67 6
    public function getCurrentDeviceProperties()
68
    {
69 6
        if ($properties = $this->detector->detect()) {
70 6
            $ua = $this->getUserAgentParser();
71
72 6
            $properties['platform']         = $ua->getOperatingSystemFamily();
73 6
            $properties['platform_version'] = $ua->getOperatingSystemVersion();
74 3
        }
75
76 6
        return $properties;
77
    }
78
}
79