1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UAParser\Result as UAResult; |
5
|
|
|
use UAParser\Result\Result as UAParserResult; |
6
|
|
|
use UserAgentParser\Exception; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
class YzalisUAParser extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
protected $detectionCapabilities = [ |
12
|
|
|
|
13
|
|
|
'browser' => [ |
14
|
|
|
'name' => true, |
15
|
|
|
'version' => true, |
16
|
|
|
], |
17
|
|
|
|
18
|
|
|
'renderingEngine' => [ |
19
|
|
|
'name' => true, |
20
|
|
|
'version' => true, |
21
|
|
|
], |
22
|
|
|
|
23
|
|
|
'operatingSystem' => [ |
24
|
|
|
'name' => true, |
25
|
|
|
'version' => true, |
26
|
|
|
], |
27
|
|
|
|
28
|
|
|
'device' => [ |
29
|
|
|
'model' => true, |
30
|
|
|
'brand' => true, |
31
|
|
|
'type' => true, |
32
|
|
|
'isMobile' => false, |
33
|
|
|
'isTouch' => false, |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
'bot' => [ |
37
|
|
|
'isBot' => false, |
38
|
|
|
'name' => false, |
39
|
|
|
'type' => false, |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
protected $defaultValues = [ |
44
|
|
|
'Other', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
private $parser; |
48
|
|
|
|
49
|
1 |
|
public function getName() |
50
|
|
|
{ |
51
|
1 |
|
return 'YzalisUAParser'; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function getComposerPackageName() |
55
|
|
|
{ |
56
|
2 |
|
return 'yzalis/ua-parser'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* @param \UAParser\UAParser $parser |
62
|
|
|
*/ |
63
|
7 |
|
public function setParser(\UAParser\UAParser $parser = null) |
64
|
|
|
{ |
65
|
7 |
|
$this->parser = $parser; |
66
|
7 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @return \UAParser\UAParser |
71
|
|
|
*/ |
72
|
7 |
|
public function getParser() |
73
|
|
|
{ |
74
|
7 |
|
if ($this->parser !== null) { |
75
|
7 |
|
return $this->parser; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$this->parser = new \UAParser\UAParser(); |
79
|
|
|
|
80
|
1 |
|
return $this->parser; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
* @param UAParserResult $resultRaw |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
6 |
|
private function hasResult(UAParserResult $resultRaw) |
90
|
|
|
{ |
91
|
|
|
/* @var $browserRaw \UAParser\Result\BrowserResult */ |
92
|
6 |
|
$browserRaw = $resultRaw->getBrowser(); |
93
|
|
|
|
94
|
6 |
|
if ($browserRaw !== null && $this->isRealResult($browserRaw->getFamily()) === true) { |
95
|
1 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/* @var $osRaw \UAParser\Result\OperatingSystemResult */ |
99
|
5 |
|
$osRaw = $resultRaw->getOperatingSystem(); |
100
|
|
|
|
101
|
5 |
|
if ($osRaw !== null && $this->isRealResult($osRaw->getFamily()) === true) { |
102
|
1 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/* @var $deviceRaw \UAParser\Result\DeviceResult */ |
106
|
4 |
|
$deviceRaw = $resultRaw->getDevice(); |
107
|
|
|
|
108
|
4 |
|
if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getConstructor()) === true) { |
109
|
1 |
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getModel()) === true) { |
113
|
1 |
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* @param Model\Browser $browser |
122
|
|
|
* @param UAResult\BrowserResult $browserRaw |
123
|
|
|
*/ |
124
|
4 |
View Code Duplication |
private function hydrateBrowser(Model\Browser $browser, UAResult\BrowserResult $browserRaw) |
|
|
|
|
125
|
|
|
{ |
126
|
4 |
|
if ($this->isRealResult($browserRaw->getFamily()) === true) { |
127
|
1 |
|
$browser->setName($browserRaw->getFamily()); |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
if ($this->isRealResult($browserRaw->getVersionString()) === true) { |
131
|
1 |
|
$browser->getVersion()->setComplete($browserRaw->getVersionString()); |
132
|
|
|
} |
133
|
4 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* |
137
|
|
|
* @param Model\RenderingEngine $engine |
138
|
|
|
* @param UAResult\RenderingEngineResult $renderingEngineRaw |
139
|
|
|
*/ |
140
|
4 |
View Code Duplication |
private function hydrateRenderingEngine(Model\RenderingEngine $engine, UAResult\RenderingEngineResult $renderingEngineRaw) |
|
|
|
|
141
|
|
|
{ |
142
|
4 |
|
if ($this->isRealResult($renderingEngineRaw->getFamily()) === true) { |
143
|
1 |
|
$engine->setName($renderingEngineRaw->getFamily()); |
144
|
|
|
} |
145
|
|
|
|
146
|
4 |
|
if ($this->isRealResult($renderingEngineRaw->getVersion()) === true) { |
147
|
1 |
|
$engine->getVersion()->setComplete($renderingEngineRaw->getVersion()); |
148
|
|
|
} |
149
|
4 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @param Model\OperatingSystem $os |
154
|
|
|
* @param UAResult\OperatingSystemResult $osRaw |
155
|
|
|
*/ |
156
|
4 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, UAResult\OperatingSystemResult $osRaw) |
157
|
|
|
{ |
158
|
4 |
|
if ($this->isRealResult($osRaw->getFamily()) === true) { |
159
|
1 |
|
$os->setName($osRaw->getFamily()); |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
if ($this->isRealResult($osRaw->getMajor()) === true) { |
163
|
1 |
|
$os->getVersion()->setMajor($osRaw->getMajor()); |
164
|
|
|
|
165
|
1 |
|
if ($this->isRealResult($osRaw->getMinor()) === true) { |
166
|
1 |
|
$os->getVersion()->setMinor($osRaw->getMinor()); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
if ($this->isRealResult($osRaw->getPatch()) === true) { |
170
|
1 |
|
$os->getVersion()->setPatch($osRaw->getPatch()); |
171
|
|
|
} |
172
|
|
|
} |
173
|
4 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
* @param Model\UserAgent $device |
178
|
|
|
* @param UAResult\DeviceResult $deviceRaw |
179
|
|
|
*/ |
180
|
4 |
|
private function hydrateDevice(Model\Device $device, UAResult\DeviceResult $deviceRaw) |
181
|
|
|
{ |
182
|
4 |
|
if ($this->isRealResult($deviceRaw->getModel()) === true) { |
183
|
2 |
|
$device->setModel($deviceRaw->getModel()); |
184
|
|
|
} |
185
|
|
|
|
186
|
4 |
|
if ($this->isRealResult($deviceRaw->getConstructor()) === true) { |
187
|
1 |
|
$device->setBrand($deviceRaw->getConstructor()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// removed desktop type, since it's a default value and not really detected |
191
|
4 |
|
if ($this->isRealResult($deviceRaw->getType()) === true && $deviceRaw->getType() !== 'desktop') { |
192
|
2 |
|
$device->setType($deviceRaw->getType()); |
193
|
|
|
} |
194
|
4 |
|
} |
195
|
|
|
|
196
|
6 |
|
public function parse($userAgent, array $headers = []) |
197
|
|
|
{ |
198
|
6 |
|
$parser = $this->getParser(); |
199
|
|
|
|
200
|
|
|
/* @var $resultRaw \UAParser\Result\Result */ |
201
|
6 |
|
$resultRaw = $parser->parse($userAgent); |
202
|
|
|
|
203
|
|
|
/* |
204
|
|
|
* No result found? |
205
|
|
|
*/ |
206
|
6 |
|
if ($this->hasResult($resultRaw) !== true) { |
207
|
2 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/* @var $emailRaw \UAParser\Result\EmailClientResult */ |
211
|
|
|
// currently not used...any idea to implement it? |
212
|
|
|
|
213
|
|
|
/* |
214
|
|
|
* Hydrate the model |
215
|
|
|
*/ |
216
|
4 |
|
$result = new Model\UserAgent(); |
217
|
4 |
|
$result->setProviderResultRaw($resultRaw); |
218
|
|
|
|
219
|
|
|
/* |
220
|
|
|
* Bot detection is currently not possible |
221
|
|
|
*/ |
222
|
|
|
|
223
|
|
|
/* |
224
|
|
|
* hydrate the result |
225
|
|
|
*/ |
226
|
4 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw->getBrowser()); |
227
|
4 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw->getRenderingEngine()); |
228
|
4 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->getOperatingSystem()); |
229
|
4 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw->getDevice()); |
230
|
|
|
|
231
|
4 |
|
return $result; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
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.