1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
5
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
6
|
|
|
use UserAgentParser\Model; |
7
|
|
|
|
8
|
|
|
class DonatjUAParser extends AbstractProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Name of the provider |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = 'DonatjUAParser'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Homepage of the provider |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $homepage = 'https://github.com/donatj/PhpUserAgent'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Composer package name |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $packageName = 'donatj/phpuseragentparser'; |
30
|
|
|
|
31
|
|
|
protected $detectionCapabilities = [ |
32
|
|
|
|
33
|
|
|
'browser' => [ |
34
|
|
|
'name' => true, |
35
|
|
|
'version' => true, |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
'renderingEngine' => [ |
39
|
|
|
'name' => false, |
40
|
|
|
'version' => false, |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
'operatingSystem' => [ |
44
|
|
|
'name' => false, |
45
|
|
|
'version' => false, |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
'device' => [ |
49
|
|
|
'model' => false, |
50
|
|
|
'brand' => false, |
51
|
|
|
'type' => false, |
52
|
|
|
'isMobile' => false, |
53
|
|
|
'isTouch' => false, |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
'bot' => [ |
57
|
|
|
'isBot' => false, |
58
|
|
|
'name' => false, |
59
|
|
|
'type' => false, |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
9 |
|
public function __construct() |
64
|
|
|
{ |
65
|
9 |
View Code Duplication |
if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) { |
|
|
|
|
66
|
1 |
|
throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider'); |
67
|
|
|
} |
68
|
8 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* @param array $resultRaw |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
2 |
|
private function hasResult(array $resultRaw) |
77
|
|
|
{ |
78
|
2 |
|
if ($this->isRealResult($resultRaw['browser'])) { |
79
|
1 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @param Model\Browser $browser |
88
|
|
|
* @param array $resultRaw |
89
|
|
|
*/ |
90
|
1 |
|
private function hydrateBrowser(Model\Browser $browser, array $resultRaw) |
91
|
|
|
{ |
92
|
1 |
|
if ($this->isRealResult($resultRaw['browser']) === true) { |
93
|
1 |
|
$browser->setName($resultRaw['browser']); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
if ($this->isRealResult($resultRaw['version']) === true) { |
97
|
1 |
|
$browser->getVersion()->setComplete($resultRaw['version']); |
98
|
1 |
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
2 |
|
public function parse($userAgent, array $headers = []) |
102
|
|
|
{ |
103
|
2 |
|
$resultRaw = parse_user_agent($userAgent); |
104
|
|
|
|
105
|
2 |
|
if ($this->hasResult($resultRaw) !== true) { |
106
|
1 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* Hydrate the model |
111
|
|
|
*/ |
112
|
1 |
|
$result = new Model\UserAgent(); |
113
|
1 |
|
$result->setProviderResultRaw($resultRaw); |
114
|
|
|
|
115
|
|
|
/* |
116
|
|
|
* Bot detection - is currently not possible! |
117
|
|
|
*/ |
118
|
|
|
|
119
|
|
|
/* |
120
|
|
|
* hydrate the result |
121
|
|
|
*/ |
122
|
1 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
123
|
|
|
// renderingEngine not available |
124
|
|
|
// os is mixed with device informations |
125
|
|
|
// device is mixed with os |
126
|
|
|
|
127
|
1 |
|
return $result; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.