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