Completed
Push — master ( 9a7d92...eb3a6f )
by ARCANEDEV
09:44
created

DeviceDetector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 84
rs 10
c 0
b 0
f 0
ccs 17
cts 18
cp 0.9444

6 Methods

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