Completed
Push — master ( 8d9645...356eb6 )
by Gabriel
05:31
created

OsDetector::checkOpenBSD()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 3
cts 6
cp 0.5
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2.5
1
<?php
2
3
namespace Sinergi\BrowserDetector;
4
5
class OsDetector implements DetectorInterface
6
{
7
    /**
8
     * Determine the user's operating system.
9
     *
10
     * @param Os $os
11
     * @param UserAgent $userAgent
12
     *
13
     * @return bool
14
     */
15 9
    public static function detect(Os $os, UserAgent $userAgent)
16
    {
17 9
        $os->setName($os::UNKNOWN);
18 9
        $os->setVersion($os::VERSION_UNKNOWN);
19 9
        $os->setIsMobile(false);
20
21 9
        self::checkMobileBrowsers($os, $userAgent);
22
23
        return (
24
            // Chrome OS before OS X
25 9
            self::checkChromeOs($os, $userAgent) ||
26
            // iOS before OS X
27 9
            self::checkIOS($os, $userAgent) ||
28 7
            self::checkOSX($os, $userAgent) ||
29 5
            self::checkSymbOS($os, $userAgent) ||
30 5
            self::checkWindows($os, $userAgent) ||
31 3
            self::checkWindowsPhone($os, $userAgent) ||
32 3
            self::checkFreeBSD($os, $userAgent) ||
33 3
            self::checkOpenBSD($os, $userAgent) ||
34 3
            self::checkNetBSD($os, $userAgent) ||
35 3
            self::checkOpenSolaris($os, $userAgent) ||
36 3
            self::checkSunOS($os, $userAgent) ||
37 3
            self::checkOS2($os, $userAgent) ||
38 3
            self::checkBeOS($os, $userAgent) ||
39
            // Android before Linux
40 3
            self::checkAndroid($os, $userAgent) ||
41 3
            self::checkLinux($os, $userAgent) ||
42 3
            self::checkNokia($os, $userAgent) ||
43 3
            self::checkBlackBerry($os, $userAgent)
44 9
        );
45
    }
46
47
    /**
48
     * Determine if the user's browser is on a mobile device.
49
     *
50
     * @param Os $os
51
     * @param UserAgent $userAgent
52
     *
53
     * @return bool
54
     */
55 9
    public static function checkMobileBrowsers(Os $os, UserAgent $userAgent)
56
    {
57
        // Check for Opera Mini
58 9
        if (stripos($userAgent->getUserAgentString(), 'opera mini') !== false) {
59
            $os->setIsMobile(true);
60
        } // Set is mobile for Pocket IE
61 9
        elseif (stripos($userAgent->getUserAgentString(), 'mspie') !== false ||
62 9
            stripos($userAgent->getUserAgentString(), 'pocket') !== false) {
63
            $os->setIsMobile(true);
64
        }
65 9
    }
66
67
    /**
68
     * Determine if the user's operating system is iOS.
69
     *
70
     * @param Os $os
71
     * @param UserAgent $userAgent
72
     *
73
     * @return bool
74
     */
75 9
    private static function checkIOS(Os $os, UserAgent $userAgent)
76
    {
77 9
        if (stripos($userAgent->getUserAgentString(), 'CPU OS') !== false ||
78 8
            stripos($userAgent->getUserAgentString(), 'iPhone OS') !== false &&
79 9
            stripos($userAgent->getUserAgentString(), 'OS X')) {
80 3
            $os->setName($os::IOS);
81 3
            if (preg_match('/CPU( iPhone)? OS ([\d_]*)/i', $userAgent->getUserAgentString(), $matches)) {
82 3
                $os->setVersion(str_replace('_', '.', $matches[2]));
83 3
            }
84 3
            $os->setIsMobile(true);
85
86 3
            return true;
87
        }
88
89 7
        return false;
90
    }
91
92
    /**
93
     * Determine if the user's operating system is Chrome OS.
94
     *
95
     * @param Os $os
96
     * @param UserAgent $userAgent
97
     *
98
     * @return bool
99
     */
100 9 View Code Duplication
    private static function checkChromeOs(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
    {
102 9
        if (stripos($userAgent->getUserAgentString(), 'CrOS') !== false) {
103 1
            $os->setName($os::CHROME_OS);
104 1
            if (preg_match('/Chrome\/([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
105 1
                $os->setVersion($matches[1]);
106 1
            }
107 1
            return true;
108
        }
109
110 9
        return false;
111
    }
112
113
    /**
114
     * Determine if the user's operating system is OS X.
115
     *
116
     * @param Os $os
117
     * @param UserAgent $userAgent
118
     *
119
     * @return bool
120
     */
121 7 View Code Duplication
    private static function checkOSX(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
122
    {
123 7
        if (stripos($userAgent->getUserAgentString(), 'OS X') !== false) {
124 3
            $os->setName($os::OSX);
125 3
            if (preg_match('/OS X ([\d\._]*)/i', $userAgent->getUserAgentString(), $matches)) {
126 3
                if (isset($matches[1])) {
127 3
                    $os->setVersion(str_replace('_', '.', $matches[1]));
128 3
                }
129 3
            }
130
131 3
            return true;
132
        }
133
134 5
        return false;
135
    }
136
137
    /**
138
     * Determine if the user's operating system is Windows.
139
     *
140
     * @param Os $os
141
     * @param UserAgent $userAgent
142
     *
143
     * @return bool
144
     */
145 5
    private static function checkWindows(Os $os, UserAgent $userAgent)
146
    {
147 5
        if (stripos($userAgent->getUserAgentString(), 'Windows NT') !== false) {
148 3
            $os->setName($os::WINDOWS);
149
            // Windows version
150 3
            if (preg_match('/Windows NT ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
151 3
                if (isset($matches[1])) {
152 3
                    switch (str_replace('_', '.', $matches[1])) {
153 3
                        case '6.3':
154
                            $os->setVersion('8.1');
155
                            break;
156 3
                        case '6.2':
157 1
                            $os->setVersion('8');
158 1
                            break;
159 3
                        case '6.1':
160 3
                            $os->setVersion('7');
161 3
                            break;
162 1
                        case '6.0':
163
                            $os->setVersion('Vista');
164
                            break;
165 1
                        case '5.2':
166 1
                        case '5.1':
167
                            $os->setVersion('XP');
168
                            break;
169 1
                        case '5.01':
170 1
                        case '5.0':
171
                            $os->setVersion('2000');
172
                            break;
173 1
                        case '4.0':
174
                            $os->setVersion('NT 4.0');
175
                            break;
176 1
                        default:
177 1
                            if ((float)$matches[1] >= 10.0) {
178 1
                                $os->setVersion($matches[1]);
179 1
                            }
180 1
                            break;
181 3
                    }
182 3
                }
183 3
            }
184
185 3
            return true;
186
        } // Windows Me, Windows 98, Windows 95, Windows CE
187 3
        elseif (preg_match(
188 3
            '/(Windows 98; Win 9x 4\.90|Windows 98|Windows 95|Windows CE)/i',
189 3
            $userAgent->getUserAgentString(),
190
            $matches
191 3
        )) {
192
            $os->setName($os::WINDOWS);
193
            switch (strtolower($matches[0])) {
194
                case 'windows 98; win 9x 4.90':
195
                    $os->setVersion('Me');
196
                    break;
197
                case 'windows 98':
198
                    $os->setVersion('98');
199
                    break;
200
                case 'windows 95':
201
                    $os->setVersion('95');
202
                    break;
203
                case 'windows ce':
204
                    $os->setVersion('CE');
205
                    break;
206
            }
207
208
            return true;
209
        }
210
211 3
        return false;
212
    }
213
    
214
    /**
215
     * Determine if the user's operating system is Windows Phone.
216
     *
217
     * @param Os $os
218
     * @param UserAgent $userAgent
219
     *
220
     * @return bool
221
     */
222 3 View Code Duplication
    private static function checkWindowsPhone(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
223
    {
224 3
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
225
            $os->setName($os::WINDOWS_PHONE);
226
            // Windows version
227
            if (preg_match('/Windows Phone ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
228
                if (isset($matches[1])) {
229
                    $os->setVersion((float)$matches[1]);
230
                }
231
            }
232
    
233
            return true;
234
        }
235 3
        return false;
236
    }
237
238
    /**
239
     * Determine if the user's operating system is SymbOS.
240
     *
241
     * @param Os $os
242
     * @param UserAgent $userAgent
243
     *
244
     * @return bool
245
     */
246 5
    private static function checkSymbOS(Os $os, UserAgent $userAgent)
247
    {
248 5
        if (stripos($userAgent->getUserAgentString(), 'SymbOS') !== false) {
249
            $os->setName($os::SYMBOS);
250
251
            return true;
252
        }
253
254 5
        return false;
255
    }
256
257
    /**
258
     * Determine if the user's operating system is Linux.
259
     *
260
     * @param Os $os
261
     * @param UserAgent $userAgent
262
     *
263
     * @return bool
264
     */
265 3 View Code Duplication
    private static function checkLinux(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
266
    {
267 3
        if (stripos($userAgent->getUserAgentString(), 'Linux') !== false) {
268 1
            $os->setVersion($os::VERSION_UNKNOWN);
269 1
            $os->setName($os::LINUX);
270
271 1
            return true;
272
        }
273
274 3
        return false;
275
    }
276
277
    /**
278
     * Determine if the user's operating system is Nokia.
279
     *
280
     * @param Os $os
281
     * @param UserAgent $userAgent
282
     *
283
     * @return bool
284
     */
285 3 View Code Duplication
    private static function checkNokia(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
286
    {
287 3
        if (stripos($userAgent->getUserAgentString(), 'Nokia') !== false) {
288
            $os->setVersion($os::VERSION_UNKNOWN);
289
            $os->setName($os::NOKIA);
290
            $os->setIsMobile(true);
291
292
            return true;
293
        }
294
295 3
        return false;
296
    }
297
298
    /**
299
     * Determine if the user's operating system is BlackBerry.
300
     *
301
     * @param Os $os
302
     * @param UserAgent $userAgent
303
     *
304
     * @return bool
305
     */
306 3
    private static function checkBlackBerry(Os $os, UserAgent $userAgent)
307
    {
308 3
        if (stripos($userAgent->getUserAgentString(), 'BlackBerry') !== false) {
309 1
            $os->setVersion($os::VERSION_UNKNOWN);
310 1
            $os->setName($os::BLACKBERRY);
311 1
            $os->setIsMobile(true);
312
313 1
            return true;
314 2
        } elseif (stripos($userAgent->getUserAgentString(), 'BB10') !== false) {
315 1
            $aresult = explode('Version/10.', $userAgent->getUserAgentString());
316 1
            if (isset($aresult[1])) {
317 1
                $aversion = explode(' ', $aresult[1]);
318 1
                $os->setVersion('10.' . $aversion[0]);
319 1
            } else {
320
                $os->setVersion('10');
321
            }
322 1
            $os->setName($os::BLACKBERRY);
323 1
            $os->setIsMobile(true);
324
325 1
            return true;
326
        }
327
328 1
        return false;
329
    }
330
331
    /**
332
     * Determine if the user's operating system is Android.
333
     *
334
     * @param Os $os
335
     * @param UserAgent $userAgent
336
     *
337
     * @return bool
338
     */
339 3
    private static function checkAndroid(Os $os, UserAgent $userAgent)
340
    {
341 3
        if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
342
            if (preg_match('/Android ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
343
                if (isset($matches[1])) {
344
                    $os->setVersion($matches[1]);
345
                }
346
            } else {
347
                $os->setVersion($os::VERSION_UNKNOWN);
348
            }
349
            $os->setName($os::ANDROID);
350
            $os->setIsMobile(true);
351
352
            return true;
353
        }
354
355 3
        return false;
356
    }
357
358
    /**
359
     * Determine if the user's operating system is FreeBSD.
360
     *
361
     * @param Os $os
362
     * @param UserAgent $userAgent
363
     *
364
     * @return bool
365
     */
366 3 View Code Duplication
    private static function checkFreeBSD(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
367
    {
368 3
        if (stripos($userAgent->getUserAgentString(), 'FreeBSD') !== false) {
369
            $os->setVersion($os::VERSION_UNKNOWN);
370
            $os->setName($os::FREEBSD);
371
372
            return true;
373
        }
374
375 3
        return false;
376
    }
377
378
    /**
379
     * Determine if the user's operating system is OpenBSD.
380
     *
381
     * @param Os $os
382
     * @param UserAgent $userAgent
383
     *
384
     * @return bool
385
     */
386 3 View Code Duplication
    private static function checkOpenBSD(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
387
    {
388 3
        if (stripos($userAgent->getUserAgentString(), 'OpenBSD') !== false) {
389
            $os->setVersion($os::VERSION_UNKNOWN);
390
            $os->setName($os::OPENBSD);
391
392
            return true;
393
        }
394
395 3
        return false;
396
    }
397
398
    /**
399
     * Determine if the user's operating system is SunOS.
400
     *
401
     * @param Os $os
402
     * @param UserAgent $userAgent
403
     *
404
     * @return bool
405
     */
406 3 View Code Duplication
    private static function checkSunOS(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
407
    {
408 3
        if (stripos($userAgent->getUserAgentString(), 'SunOS') !== false) {
409
            $os->setVersion($os::VERSION_UNKNOWN);
410
            $os->setName($os::SUNOS);
411
412
            return true;
413
        }
414
415 3
        return false;
416
    }
417
418
    /**
419
     * Determine if the user's operating system is NetBSD.
420
     *
421
     * @param Os $os
422
     * @param UserAgent $userAgent
423
     *
424
     * @return bool
425
     */
426 3 View Code Duplication
    private static function checkNetBSD(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
427
    {
428 3
        if (stripos($userAgent->getUserAgentString(), 'NetBSD') !== false) {
429
            $os->setVersion($os::VERSION_UNKNOWN);
430
            $os->setName($os::NETBSD);
431
432
            return true;
433
        }
434
435 3
        return false;
436
    }
437
438
    /**
439
     * Determine if the user's operating system is OpenSolaris.
440
     *
441
     * @param Os $os
442
     * @param UserAgent $userAgent
443
     *
444
     * @return bool
445
     */
446 3 View Code Duplication
    private static function checkOpenSolaris(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
447
    {
448 3
        if (stripos($userAgent->getUserAgentString(), 'OpenSolaris') !== false) {
449
            $os->setVersion($os::VERSION_UNKNOWN);
450
            $os->setName($os::OPENSOLARIS);
451
452
            return true;
453
        }
454
455 3
        return false;
456
    }
457
458
    /**
459
     * Determine if the user's operating system is OS2.
460
     *
461
     * @param Os $os
462
     * @param UserAgent $userAgent
463
     *
464
     * @return bool
465
     */
466 3 View Code Duplication
    private static function checkOS2(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
467
    {
468 3
        if (stripos($userAgent->getUserAgentString(), 'OS\/2') !== false) {
469
            $os->setVersion($os::VERSION_UNKNOWN);
470
            $os->setName($os::OS2);
471
472
            return true;
473
        }
474
475 3
        return false;
476
    }
477
478
    /**
479
     * Determine if the user's operating system is BeOS.
480
     *
481
     * @param Os $os
482
     * @param UserAgent $userAgent
483
     *
484
     * @return bool
485
     */
486 3 View Code Duplication
    private static function checkBeOS(Os $os, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
487
    {
488 3
        if (stripos($userAgent->getUserAgentString(), 'BeOS') !== false) {
489
            $os->setVersion($os::VERSION_UNKNOWN);
490
            $os->setName($os::BEOS);
491
492
            return true;
493
        }
494
495 3
        return false;
496
    }
497
}
498