These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace Arcanedev\LaravelTracker\Parsers; |
||
2 | |||
3 | use Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser as UserAgentParserContract; |
||
4 | |||
5 | /** |
||
6 | * Class UserAgentParser |
||
7 | * |
||
8 | * @package Arcanedev\LaravelTracker\Parsers |
||
9 | * @author ARCANEDEV <[email protected]> |
||
10 | */ |
||
11 | class UserAgentParser implements UserAgentParserContract |
||
12 | { |
||
13 | /* ------------------------------------------------------------------------------------------------ |
||
14 | | Properties |
||
15 | | ------------------------------------------------------------------------------------------------ |
||
16 | */ |
||
17 | /** @var \UAParser\Parser */ |
||
18 | protected $parser; |
||
19 | |||
20 | /** @var string */ |
||
21 | public $basePath; |
||
22 | |||
23 | /* ------------------------------------------------------------------------------------------------ |
||
24 | | Constructor |
||
25 | | ------------------------------------------------------------------------------------------------ |
||
26 | */ |
||
27 | /** |
||
28 | * UserAgentParser constructor. |
||
29 | * |
||
30 | * @param \UAParser\Parser $parser |
||
31 | * @param string $basePath |
||
32 | * @param null $userAgent |
||
33 | */ |
||
34 | 30 | public function __construct($parser, $basePath, $userAgent = null) |
|
0 ignored issues
–
show
|
|||
35 | { |
||
36 | 30 | if ( ! $userAgent && isset($_SERVER['HTTP_USER_AGENT'])) { |
|
37 | $userAgent = $_SERVER['HTTP_USER_AGENT']; |
||
38 | } |
||
39 | |||
40 | 30 | $this->parser = $parser->parse($userAgent); |
|
41 | 30 | $this->basePath = $basePath; |
|
42 | 30 | } |
|
43 | |||
44 | /* ------------------------------------------------------------------------------------------------ |
||
45 | | Main Functions |
||
46 | | ------------------------------------------------------------------------------------------------ |
||
47 | */ |
||
48 | /** |
||
49 | * Get the OS version. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | 18 | public function getOperatingSystemVersion() |
|
54 | { |
||
55 | 18 | $os = $this->getOperatingSystem(); |
|
56 | |||
57 | 18 | return $this->prepareVersion([$os->major, $os->minor, $os->patch]); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get the OS Family. |
||
62 | * |
||
63 | * @return string|null |
||
64 | */ |
||
65 | 18 | public function getOperatingSystemFamily() |
|
66 | { |
||
67 | try { |
||
68 | 18 | return $this->parser->os->family; |
|
69 | } |
||
70 | catch (\Exception $e) { |
||
71 | return null; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get the browser. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | 18 | public function getBrowser() |
|
81 | { |
||
82 | 18 | return $this->getUserAgent()->family; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get the user agent version. |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 18 | public function getUserAgentVersion() |
|
91 | { |
||
92 | 18 | $ua = $this->getUserAgent(); |
|
93 | |||
94 | 18 | return $this->prepareVersion([$ua->major, $ua->minor, $ua->patch]); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get the original user agent. |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 18 | public function getOriginalUserAgent() |
|
103 | { |
||
104 | 18 | return $this->parser->originalUserAgent; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get the user agent. |
||
109 | * |
||
110 | * @return \UAParser\Result\UserAgent |
||
111 | */ |
||
112 | 18 | public function getUserAgent() |
|
113 | { |
||
114 | 18 | return $this->parser->ua; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get the operating system. |
||
119 | * |
||
120 | * @return \UAParser\Result\OperatingSystem |
||
121 | */ |
||
122 | 18 | public function getOperatingSystem() |
|
123 | { |
||
124 | 18 | return $this->parser->os; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get the device. |
||
129 | * |
||
130 | * @return \UAParser\Result\Device |
||
131 | */ |
||
132 | public function getDevice() |
||
133 | { |
||
134 | return $this->parser->device; |
||
135 | } |
||
136 | |||
137 | /* ------------------------------------------------------------------------------------------------ |
||
138 | | Other Functions |
||
139 | | ------------------------------------------------------------------------------------------------ |
||
140 | */ |
||
141 | /** |
||
142 | * Prepare the version. |
||
143 | * |
||
144 | * @param array $version |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | 24 | protected function prepareVersion(array $version) |
|
149 | { |
||
150 | 24 | return implode('.', array_filter($version)); |
|
151 | } |
||
152 | } |
||
153 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: