1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use DeviceDetector\DeviceDetector; |
5
|
|
|
use UserAgentParser\Exception; |
6
|
|
|
use UserAgentParser\Model; |
7
|
|
|
|
8
|
|
|
class PiwikDeviceDetector extends AbstractProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Name of the provider |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = 'PiwikDeviceDetector'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Homepage of the provider |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $homepage = 'https://github.com/piwik/device-detector'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Composer package name |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $packageName = 'piwik/device-detector'; |
30
|
|
|
|
31
|
|
|
protected $detectionCapabilities = [ |
32
|
|
|
|
33
|
|
|
'browser' => [ |
34
|
|
|
'name' => true, |
35
|
|
|
'version' => true, |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
'renderingEngine' => [ |
39
|
|
|
'name' => true, |
40
|
|
|
'version' => false, |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
'operatingSystem' => [ |
44
|
|
|
'name' => true, |
45
|
|
|
'version' => true, |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
'device' => [ |
49
|
|
|
'model' => true, |
50
|
|
|
'brand' => true, |
51
|
|
|
'type' => true, |
52
|
|
|
'isMobile' => true, |
53
|
|
|
'isTouch' => true, |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
'bot' => [ |
57
|
|
|
'isBot' => true, |
58
|
|
|
'name' => true, |
59
|
|
|
'type' => true, |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
protected $defaultValues = [ |
64
|
|
|
DeviceDetector::UNKNOWN, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @var DeviceDetector |
70
|
|
|
*/ |
71
|
|
|
private $parser; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
* @param DeviceDetector $parser |
76
|
|
|
* @throws Exception\PackageNotLoaded |
77
|
|
|
*/ |
78
|
13 |
View Code Duplication |
public function __construct(DeviceDetector $parser = null) |
|
|
|
|
79
|
|
|
{ |
80
|
13 |
|
if (! class_exists('DeviceDetector\Cache\StaticCache', true)) { |
81
|
1 |
|
throw new Exception\PackageNotLoadedException('You need to install ' . $this->getHomepage() . ' to use this provider'); |
82
|
|
|
} |
83
|
|
|
|
84
|
12 |
|
$this->parser = $parser; |
85
|
12 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* @return DeviceDetector |
90
|
|
|
*/ |
91
|
6 |
|
public function getParser() |
92
|
|
|
{ |
93
|
6 |
|
if ($this->parser !== null) { |
94
|
6 |
|
return $this->parser; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$this->parser = new DeviceDetector(); |
98
|
|
|
|
99
|
1 |
|
return $this->parser; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
* @param DeviceDetector $dd |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
4 |
|
private function getResultRaw(DeviceDetector $dd) |
109
|
|
|
{ |
110
|
|
|
$raw = [ |
111
|
4 |
|
'client' => $dd->getClient(), |
112
|
4 |
|
'operatingSystem' => $dd->getOs(), |
113
|
|
|
|
114
|
|
|
'device' => [ |
115
|
4 |
|
'brand' => $dd->getBrand(), |
116
|
4 |
|
'brandName' => $dd->getBrandName(), |
117
|
|
|
|
118
|
4 |
|
'model' => $dd->getModel(), |
119
|
|
|
|
120
|
4 |
|
'device' => $dd->getDevice(), |
121
|
4 |
|
'deviceName' => $dd->getDeviceName(), |
122
|
4 |
|
], |
123
|
|
|
|
124
|
4 |
|
'bot' => $dd->getBot(), |
125
|
|
|
|
126
|
|
|
'extra' => [ |
127
|
4 |
|
'isBot' => $dd->isBot(), |
128
|
|
|
|
129
|
|
|
// client |
130
|
4 |
|
'isBrowser' => $dd->isBrowser(), |
131
|
4 |
|
'isFeedReader' => $dd->isFeedReader(), |
132
|
4 |
|
'isMobileApp' => $dd->isMobileApp(), |
133
|
4 |
|
'isPIM' => $dd->isPIM(), |
134
|
4 |
|
'isLibrary' => $dd->isLibrary(), |
135
|
4 |
|
'isMediaPlayer' => $dd->isMediaPlayer(), |
136
|
|
|
|
137
|
|
|
// deviceType |
138
|
4 |
|
'isCamera' => $dd->isCamera(), |
139
|
4 |
|
'isCarBrowser' => $dd->isCarBrowser(), |
140
|
4 |
|
'isConsole' => $dd->isConsole(), |
141
|
4 |
|
'isFeaturePhone' => $dd->isFeaturePhone(), |
142
|
4 |
|
'isPhablet' => $dd->isPhablet(), |
143
|
4 |
|
'isPortableMediaPlayer' => $dd->isPortableMediaPlayer(), |
144
|
4 |
|
'isSmartDisplay' => $dd->isSmartDisplay(), |
145
|
4 |
|
'isSmartphone' => $dd->isSmartphone(), |
146
|
4 |
|
'isTablet' => $dd->isTablet(), |
147
|
4 |
|
'isTV' => $dd->isTV(), |
148
|
|
|
|
149
|
|
|
// other special |
150
|
4 |
|
'isDesktop' => $dd->isDesktop(), |
151
|
4 |
|
'isMobile' => $dd->isMobile(), |
152
|
4 |
|
'isTouchEnabled' => $dd->isTouchEnabled(), |
153
|
4 |
|
], |
154
|
4 |
|
]; |
155
|
|
|
|
156
|
4 |
|
return $raw; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* |
161
|
|
|
* @param DeviceDetector $dd |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
5 |
|
private function hasResult(DeviceDetector $dd) |
166
|
|
|
{ |
167
|
5 |
|
if ($dd->isBot() === true) { |
168
|
1 |
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
4 |
|
$client = $dd->getClient(); |
172
|
4 |
|
if (isset($client['name']) && $this->isRealResult($client['name'])) { |
173
|
1 |
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
3 |
|
$os = $dd->getOs(); |
177
|
3 |
|
if (isset($os['name']) && $this->isRealResult($os['name'])) { |
178
|
1 |
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
if ($dd->getDevice() !== null) { |
182
|
1 |
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* |
190
|
|
|
* @param Model\Bot $bot |
191
|
|
|
* @param array|boolean $botRaw |
192
|
|
|
*/ |
193
|
1 |
View Code Duplication |
private function hydrateBot(Model\Bot $bot, $botRaw) |
|
|
|
|
194
|
|
|
{ |
195
|
1 |
|
$bot->setIsBot(true); |
196
|
|
|
|
197
|
1 |
|
if (isset($botRaw['name']) && $this->isRealResult($botRaw['name'])) { |
198
|
1 |
|
$bot->setName($botRaw['name']); |
199
|
|
|
} |
200
|
1 |
|
if (isset($botRaw['category']) && $this->isRealResult($botRaw['category'])) { |
201
|
1 |
|
$bot->setType($botRaw['category']); |
202
|
|
|
} |
203
|
1 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* |
207
|
|
|
* @param Model\Browser $browser |
208
|
|
|
* @param array|string $clientRaw |
209
|
|
|
*/ |
210
|
3 |
View Code Duplication |
private function hydrateBrowser(Model\Browser $browser, $clientRaw) |
|
|
|
|
211
|
|
|
{ |
212
|
3 |
|
if (isset($clientRaw['name']) && $this->isRealResult($clientRaw['name']) === true) { |
213
|
1 |
|
$browser->setName($clientRaw['name']); |
214
|
|
|
} |
215
|
|
|
|
216
|
3 |
|
if (isset($clientRaw['version']) && $this->isRealResult($clientRaw['version']) === true) { |
217
|
1 |
|
$browser->getVersion()->setComplete($clientRaw['version']); |
218
|
|
|
} |
219
|
3 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* |
223
|
|
|
* @param Model\RenderingEngine $engine |
224
|
|
|
* @param array|string $clientRaw |
225
|
|
|
*/ |
226
|
3 |
|
private function hydrateRenderingEngine(Model\RenderingEngine $engine, $clientRaw) |
227
|
|
|
{ |
228
|
3 |
|
if (isset($clientRaw['engine']) && $this->isRealResult($clientRaw['engine']) === true) { |
229
|
1 |
|
$engine->setName($clientRaw['engine']); |
230
|
|
|
} |
231
|
3 |
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* |
235
|
|
|
* @param Model\OperatingSystem $os |
236
|
|
|
* @param array|string $osRaw |
237
|
|
|
*/ |
238
|
3 |
View Code Duplication |
private function hydrateOperatingSystem(Model\OperatingSystem $os, $osRaw) |
|
|
|
|
239
|
|
|
{ |
240
|
3 |
|
if (isset($osRaw['name']) && $this->isRealResult($osRaw['name']) === true) { |
241
|
1 |
|
$os->setName($osRaw['name']); |
242
|
|
|
} |
243
|
|
|
|
244
|
3 |
|
if (isset($osRaw['version']) && $this->isRealResult($osRaw['version']) === true) { |
245
|
1 |
|
$os->getVersion()->setComplete($osRaw['version']); |
246
|
|
|
} |
247
|
3 |
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* |
251
|
|
|
* @param Model\UserAgent $device |
252
|
|
|
* @param DeviceDetector $dd |
253
|
|
|
*/ |
254
|
3 |
|
private function hydrateDevice(Model\Device $device, DeviceDetector $dd) |
255
|
|
|
{ |
256
|
3 |
|
if ($this->isRealResult($dd->getModel()) === true) { |
257
|
1 |
|
$device->setModel($dd->getModel()); |
258
|
|
|
} |
259
|
|
|
|
260
|
3 |
|
if ($this->isRealResult($dd->getBrandName()) === true) { |
261
|
1 |
|
$device->setBrand($dd->getBrandName()); |
262
|
|
|
} |
263
|
|
|
|
264
|
3 |
|
if ($this->isRealResult($dd->getDeviceName()) === true) { |
265
|
1 |
|
$device->setType($dd->getDeviceName()); |
266
|
|
|
} |
267
|
|
|
|
268
|
3 |
|
if ($dd->isMobile() === true) { |
269
|
1 |
|
$device->setIsMobile(true); |
270
|
|
|
} |
271
|
|
|
|
272
|
3 |
|
if ($dd->isTouchEnabled() === true) { |
273
|
1 |
|
$device->setIsTouch(true); |
274
|
|
|
} |
275
|
3 |
|
} |
276
|
|
|
|
277
|
5 |
|
public function parse($userAgent, array $headers = []) |
278
|
|
|
{ |
279
|
5 |
|
$dd = $this->getParser(); |
280
|
|
|
|
281
|
5 |
|
$dd->setUserAgent($userAgent); |
282
|
5 |
|
$dd->parse(); |
283
|
|
|
|
284
|
|
|
/* |
285
|
|
|
* No result found? |
286
|
|
|
*/ |
287
|
5 |
|
if ($this->hasResult($dd) !== true) { |
288
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/* |
292
|
|
|
* Hydrate the model |
293
|
|
|
*/ |
294
|
4 |
|
$result = new Model\UserAgent(); |
295
|
4 |
|
$result->setProviderResultRaw($this->getResultRaw($dd)); |
296
|
|
|
|
297
|
|
|
/* |
298
|
|
|
* Bot detection |
299
|
|
|
*/ |
300
|
4 |
|
if ($dd->isBot() === true) { |
301
|
1 |
|
$this->hydrateBot($result->getBot(), $dd->getBot()); |
302
|
|
|
|
303
|
1 |
|
return $result; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/* |
307
|
|
|
* hydrate the result |
308
|
|
|
*/ |
309
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $dd->getClient()); |
310
|
3 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $dd->getClient()); |
311
|
3 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $dd->getOs()); |
312
|
3 |
|
$this->hydrateDevice($result->getDevice(), $dd); |
313
|
|
|
|
314
|
3 |
|
return $result; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.