Completed
Push — master ( c4d057...a4831f )
by ARCANEDEV
07:52
created

DeviceDetector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 85
rs 10
c 0
b 0
f 0
ccs 0
cts 31
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A detect() 0 8 2
A getDeviceKind() 0 8 4
A isTablet() 0 4 1
A isComputer() 0 4 1
A isPhone() 0 4 2
1
<?php namespace Arcanedev\LaravelTracker\Detectors;
2
3
use Arcanedev\LaravelTracker\Contracts\Detectors\DeviceDetector as DeviceDetectorContract;
4
5
/**
6
 * Class     DeviceDetector
7
 *
8
 * @package  Arcanedev\LaravelTracker\Detectors
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class DeviceDetector implements DeviceDetectorContract
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /** @var \Jenssegers\Agent\Agent */
18
    protected $agent;
19
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Constructor
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /**
25
     * DeviceDetector constructor.
26
     *
27
     * @param  \Jenssegers\Agent\Agent  $agent
28
     */
29
    public function __construct($agent)
30
    {
31
        $this->agent = $agent;
32
    }
33
34
    /* ------------------------------------------------------------------------------------------------
35
     |  Main Functions
36
     | ------------------------------------------------------------------------------------------------
37
     */
38
    /**
39
     * Detect kind, model and mobility.
40
     *
41
     * @return array
42
     */
43
    public function detect()
44
    {
45
        return [
46
            'kind'      => $this->getDeviceKind(),
47
            'model'     => $this->agent->device() ?: '',
48
            'is_mobile' => $this->agent->isMobile(),
49
        ];
50
    }
51
52
    /**
53
     * Get the kind of device.
54
     *
55
     * @return string
56
     */
57
    public function getDeviceKind()
58
    {
59
        if ($this->isTablet())   return 'Tablet';
60
        if ($this->isPhone())    return 'Phone';
61
        if ($this->isComputer()) return 'Computer';
62
63
        return 'unavailable';
64
    }
65
66
    /**
67
     * Is this a tablet?
68
     *
69
     * @return bool
70
     */
71
    public function isTablet()
72
    {
73
        return $this->agent->isTablet();
74
    }
75
76
    /**
77
     * Is this a computer?
78
     *
79
     * @return bool
80
     */
81
    public function isComputer()
82
    {
83
        return ! $this->agent->isMobile();
84
    }
85
86
    /**
87
     * Is this a phone?
88
     *
89
     * @return bool
90
     */
91
    public function isPhone()
92
    {
93
        return ! ($this->isTablet() || $this->isComputer());
94
    }
95
}
96