Completed
Push — master ( f01045...b18803 )
by ARCANEDEV
07:38
created

DeviceDetector::getDeviceKind()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
nop 0
crap 4.128
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 24
    public function __construct($agent)
30
    {
31 24
        $this->agent = $agent;
32 24
    }
33
34
    /* ------------------------------------------------------------------------------------------------
35
     |  Main Functions
36
     | ------------------------------------------------------------------------------------------------
37
     */
38
    /**
39
     * Detect kind, model and mobility.
40
     *
41
     * @return array
42
     */
43 6
    public function detect()
44
    {
45
        return [
46 6
            'kind'      => $this->getDeviceKind(),
47 6
            'model'     => $this->agent->device() ?: '',
48 6
            'is_mobile' => $this->agent->isMobile(),
49 3
        ];
50
    }
51
52
    /**
53
     * Get the kind of device.
54
     *
55
     * @return string
56
     */
57 6
    public function getDeviceKind()
58
    {
59 6
        if ($this->isTablet())   return 'Tablet';
60 6
        if ($this->isPhone())    return 'Phone';
61 6
        if ($this->isComputer()) return 'Computer';
62
63
        return 'unavailable';
64
    }
65
66
    /**
67
     * Is this a tablet?
68
     *
69
     * @return bool
70
     */
71 6
    public function isTablet()
72
    {
73 6
        return $this->agent->isTablet();
74
    }
75
76
    /**
77
     * Is this a computer?
78
     *
79
     * @return bool
80
     */
81 6
    public function isComputer()
82
    {
83 6
        return ! $this->agent->isMobile();
84
    }
85
86
    /**
87
     * Is this a phone?
88
     *
89
     * @return bool
90
     */
91 6
    public function isPhone()
92
    {
93 6
        return ! ($this->isTablet() || $this->isComputer());
94
    }
95
}
96