DeviceDetector::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
20
    /** @var \Arcanedev\Agent\Contracts\Agent */
21
    protected $agent;
22
23
    /* -----------------------------------------------------------------
24
     |  Constructor
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
     * DeviceDetector constructor.
30
     *
31
     * @param  \Arcanedev\Agent\Contracts\Agent  $agent
32
     */
33 9
    public function __construct(Agent $agent)
34
    {
35 9
        $this->agent = $agent;
36 9
    }
37
38
    /* -----------------------------------------------------------------
39
     |  Main Methods
40
     | -----------------------------------------------------------------
41
     */
42
43
    /**
44
     * Detect kind, model and mobility.
45
     *
46
     * @return array
47
     */
48 9
    public function detect()
49
    {
50
        return [
51 9
            'kind'      => $this->getDeviceKind(),
52 9
            'model'     => $this->agent->device() ?: '',
53
        ];
54
    }
55
56
    /**
57
     * Get the kind of device.
58
     *
59
     * @return string
60
     */
61 9
    public function getDeviceKind()
62
    {
63 9
        if ($this->isTablet())
64
            return Device::KIND_TABLET;
65
66 9
        if ($this->isPhone())
67
            return Device::KIND_PHONE;
68
69 9
        if ($this->isComputer())
70 9
            return Device::KIND_COMPUTER;
71
72
        return Device::KIND_UNAVAILABLE;
73
    }
74
75
    /**
76
     * Is this a tablet?
77
     *
78
     * @return bool
79
     */
80 9
    public function isTablet()
81
    {
82 9
        return $this->agent->isTablet();
83
    }
84
85
    /**
86
     * Is this a computer?
87
     *
88
     * @return bool
89
     */
90 9
    public function isComputer()
91
    {
92 9
        return ! $this->agent->isMobile();
93
    }
94
95
    /**
96
     * Is this a phone?
97
     *
98
     * @return bool
99
     */
100 9
    public function isPhone()
101
    {
102 9
        return ! ($this->isTablet() || $this->isComputer());
103
    }
104
}
105