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
|
3 |
|
]; |
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
|
3 |
|
return Device::KIND_TABLET; |
65
|
|
|
|
66
|
9 |
|
if ($this->isPhone()) |
67
|
3 |
|
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
|
|
|
|