|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\Agent\Detectors; |
|
6
|
|
|
|
|
7
|
|
|
use Arcanedev\Agent\Contracts\Detector; |
|
8
|
|
|
use DeviceDetector\DeviceDetector as BaseDetector; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class DeviceDetector |
|
13
|
|
|
* |
|
14
|
|
|
* @package Arcanedev\Agent\Detectors |
|
15
|
|
|
* @author ARCANEDEV <[email protected]> |
|
16
|
|
|
* |
|
17
|
|
|
* @mixin \DeviceDetector\DeviceDetector |
|
18
|
|
|
*/ |
|
19
|
|
|
class DeviceDetector implements Detector |
|
20
|
|
|
{ |
|
21
|
|
|
/* ----------------------------------------------------------------- |
|
22
|
|
|
| Properties |
|
23
|
|
|
| ----------------------------------------------------------------- |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
/** @var \DeviceDetector\DeviceDetector */ |
|
27
|
|
|
protected $detector; |
|
28
|
|
|
|
|
29
|
|
|
/* ----------------------------------------------------------------- |
|
30
|
|
|
| Main Methods |
|
31
|
|
|
| ----------------------------------------------------------------- |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Handle the given request. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \Illuminate\Http\Request $request |
|
38
|
|
|
* |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
372 |
|
public function handle(Request $request): Detector |
|
42
|
|
|
{ |
|
43
|
372 |
|
$userAgent = $request->server('HTTP_USER_AGENT'); |
|
44
|
|
|
|
|
45
|
248 |
|
$this->detector = tap(new BaseDetector($userAgent), function (BaseDetector $detector) { |
|
|
|
|
|
|
46
|
372 |
|
$detector->parse(); |
|
47
|
372 |
|
}); |
|
48
|
|
|
|
|
49
|
372 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/* ----------------------------------------------------------------- |
|
53
|
|
|
| Accessors |
|
54
|
|
|
| ----------------------------------------------------------------- |
|
55
|
|
|
*/ |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the OS's name. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
42 |
|
public function osName(): string |
|
63
|
|
|
{ |
|
64
|
42 |
|
return $this->getOs('name'); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the OS's short name. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
42 |
|
public function osShortName(): string |
|
73
|
|
|
{ |
|
74
|
42 |
|
return $this->getOs('short_name'); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the OS's version. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
36 |
|
public function osVersion(): string |
|
83
|
|
|
{ |
|
84
|
36 |
|
return $this->getOs('version'); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the client name. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
72 |
|
public function clientName() |
|
93
|
|
|
{ |
|
94
|
72 |
|
return $this->getClient('name'); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the client name. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
72 |
|
public function clientShortName() |
|
103
|
|
|
{ |
|
104
|
72 |
|
return $this->getClient('short_name'); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the client's version. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
66 |
|
public function clientVersion(): string |
|
113
|
|
|
{ |
|
114
|
66 |
|
return $this->getClient('version'); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the bot's name. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
30 |
|
public function botName(): string |
|
123
|
|
|
{ |
|
124
|
30 |
|
return $this->getBot()['name'] ?? BaseDetector::UNKNOWN; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Check if it's a visitor. |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
30 |
|
public function isVisitor(): bool |
|
133
|
|
|
{ |
|
134
|
30 |
|
return ! $this->isBot(); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/* ----------------------------------------------------------------- |
|
138
|
|
|
| Check Methods |
|
139
|
|
|
| ----------------------------------------------------------------- |
|
140
|
|
|
*/ |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Check the given name matches client's name. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $name |
|
146
|
|
|
* |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
72 |
|
public function isClientName($name): bool |
|
150
|
|
|
{ |
|
151
|
72 |
|
return in_array($name, [ |
|
152
|
72 |
|
$this->clientName(), |
|
153
|
72 |
|
$this->clientShortName(), |
|
154
|
|
|
]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Check the given name matches Operating system's name. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $name |
|
161
|
|
|
* |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
42 |
|
public function isOsName($name): bool |
|
165
|
|
|
{ |
|
166
|
42 |
|
return in_array($name, [ |
|
167
|
42 |
|
$this->osName(), |
|
168
|
42 |
|
$this->osShortName(), |
|
169
|
|
|
]); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/* ----------------------------------------------------------------- |
|
173
|
|
|
| Other Methods |
|
174
|
|
|
| ----------------------------------------------------------------- |
|
175
|
|
|
*/ |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $name |
|
179
|
|
|
* @param array $params |
|
180
|
|
|
* |
|
181
|
|
|
* @return mixed |
|
182
|
|
|
*/ |
|
183
|
354 |
|
public function __call($name, $params) |
|
184
|
|
|
{ |
|
185
|
354 |
|
return call_user_func_array([$this->detector, $name], $params); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.