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