1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UserAgentParser\Exception; |
5
|
|
|
use UserAgentParser\Model; |
6
|
|
|
use Woothee\Classifier; |
7
|
|
|
use Woothee\DataSet; |
8
|
|
|
|
9
|
|
|
class Woothee extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
protected $detectionCapabilities = [ |
12
|
|
|
|
13
|
|
|
'browser' => [ |
14
|
|
|
'name' => true, |
15
|
|
|
'version' => true, |
16
|
|
|
], |
17
|
|
|
|
18
|
|
|
'renderingEngine' => [ |
19
|
|
|
'name' => false, |
20
|
|
|
'version' => false, |
21
|
|
|
], |
22
|
|
|
|
23
|
|
|
'operatingSystem' => [ |
24
|
|
|
'name' => false, |
25
|
|
|
'version' => false, |
26
|
|
|
], |
27
|
|
|
|
28
|
|
|
'device' => [ |
29
|
|
|
'model' => false, |
30
|
|
|
'brand' => false, |
31
|
|
|
'type' => true, |
32
|
|
|
'isMobile' => false, |
33
|
|
|
'isTouch' => false, |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
'bot' => [ |
37
|
|
|
'isBot' => true, |
38
|
|
|
'name' => true, |
39
|
|
|
'type' => false, |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
protected $defaultValues = [ |
44
|
|
|
DataSet::VALUE_UNKNOWN, |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
private $parser; |
48
|
|
|
|
49
|
1 |
|
public function getName() |
50
|
|
|
{ |
51
|
1 |
|
return 'Woothee'; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function getComposerPackageName() |
55
|
|
|
{ |
56
|
2 |
|
return 'woothee/woothee'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* @param Classifier $parser |
62
|
|
|
*/ |
63
|
6 |
|
public function setParser(Classifier $parser = null) |
64
|
|
|
{ |
65
|
6 |
|
$this->parser = $parser; |
66
|
6 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @return Classifier |
71
|
|
|
*/ |
72
|
6 |
|
public function getParser() |
73
|
|
|
{ |
74
|
6 |
|
if ($this->parser !== null) { |
75
|
6 |
|
return $this->parser; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$this->parser = new Classifier(); |
79
|
|
|
|
80
|
1 |
|
return $this->parser; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
* @param array $resultRaw |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
5 |
|
private function hasResult(array $resultRaw) |
90
|
|
|
{ |
91
|
5 |
|
foreach ($resultRaw as $value) { |
92
|
4 |
|
if ($this->isRealResult($value) === true) { |
93
|
4 |
|
return true; |
94
|
|
|
} |
95
|
1 |
|
} |
96
|
|
|
|
97
|
1 |
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
* @param array $resultRaw |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
4 |
|
private function isBot(array $resultRaw) |
106
|
|
|
{ |
107
|
4 |
|
if (isset($resultRaw['category']) && $resultRaw['category'] === DataSet::DATASET_CATEGORY_CRAWLER) { |
108
|
1 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
* @param Model\Bot $bot |
117
|
|
|
* @param array $resultRaw |
118
|
|
|
*/ |
119
|
1 |
|
private function hydrateBot(Model\Bot $bot, array $resultRaw) |
120
|
|
|
{ |
121
|
1 |
|
$bot->setIsBot(true); |
122
|
|
|
|
123
|
1 |
|
if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name']) === true) { |
124
|
1 |
|
$bot->setName($resultRaw['name']); |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* @param Model\Browser $browser |
131
|
|
|
* @param array $resultRaw |
132
|
|
|
*/ |
133
|
3 |
View Code Duplication |
private function hydrateBrowser(Model\Browser $browser, array $resultRaw) |
|
|
|
|
134
|
|
|
{ |
135
|
3 |
|
if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name']) === true) { |
136
|
1 |
|
$browser->setName($resultRaw['name']); |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
if (isset($resultRaw['version']) && $this->isRealResult($resultRaw['version']) === true) { |
140
|
1 |
|
$browser->getVersion()->setComplete($resultRaw['version']); |
141
|
|
|
} |
142
|
3 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* @param Model\Device $device |
147
|
|
|
* @param array $resultRaw |
148
|
|
|
*/ |
149
|
3 |
|
private function hydrateDevice(Model\Device $device, array $resultRaw) |
150
|
|
|
{ |
151
|
3 |
|
if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category']) === true) { |
152
|
2 |
|
$device->setType($resultRaw['category']); |
153
|
|
|
} |
154
|
3 |
|
} |
155
|
|
|
|
156
|
5 |
View Code Duplication |
public function parse($userAgent, array $headers = []) |
|
|
|
|
157
|
|
|
{ |
158
|
5 |
|
$parser = $this->getParser(); |
159
|
|
|
|
160
|
5 |
|
$resultRaw = $parser->parse($userAgent); |
161
|
|
|
|
162
|
|
|
/* |
163
|
|
|
* No result found? |
164
|
|
|
*/ |
165
|
5 |
|
if ($this->hasResult($resultRaw) !== true) { |
166
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/* |
170
|
|
|
* Hydrate the model |
171
|
|
|
*/ |
172
|
4 |
|
$result = new Model\UserAgent(); |
173
|
4 |
|
$result->setProviderResultRaw($resultRaw); |
174
|
|
|
|
175
|
|
|
/* |
176
|
|
|
* Bot detection |
177
|
|
|
*/ |
178
|
4 |
|
if ($this->isBot($resultRaw) === true) { |
179
|
1 |
|
$this->hydrateBot($result->getBot(), $resultRaw); |
180
|
|
|
|
181
|
1 |
|
return $result; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/* |
185
|
|
|
* hydrate the result |
186
|
|
|
*/ |
187
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
188
|
|
|
// renderingEngine not available |
189
|
|
|
// operatingSystem filled OS is mixed! Examples: iPod, iPhone, Android... |
190
|
3 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw); |
191
|
|
|
|
192
|
3 |
|
return $result; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.