Completed
Push — master ( 356eb6...32a6a0 )
by Gabriel
07:28
created

OsDetector::checkBeOS()   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
    private static function checkChromeOs(Os $os, UserAgent $userAgent)
101
    {
102 9
        if (stripos($userAgent->getUserAgentString(), ' CrOS') !== false ||
103 9
            stripos($userAgent->getUserAgentString(), 'CrOS ') !== false
104 9
        ) {
105 1
            $os->setName($os::CHROME_OS);
106 1
            if (preg_match('/Chrome\/([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
107 1
                $os->setVersion($matches[1]);
108 1
            }
109 1
            return true;
110
        }
111
112 9
        return false;
113
    }
114
115
    /**
116
     * Determine if the user's operating system is OS X.
117
     *
118
     * @param Os $os
119
     * @param UserAgent $userAgent
120
     *
121
     * @return bool
122
     */
123 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...
124
    {
125 7
        if (stripos($userAgent->getUserAgentString(), 'OS X') !== false) {
126 3
            $os->setName($os::OSX);
127 3
            if (preg_match('/OS X ([\d\._]*)/i', $userAgent->getUserAgentString(), $matches)) {
128 3
                if (isset($matches[1])) {
129 3
                    $os->setVersion(str_replace('_', '.', $matches[1]));
130 3
                }
131 3
            }
132
133 3
            return true;
134
        }
135
136 5
        return false;
137
    }
138
139
    /**
140
     * Determine if the user's operating system is Windows.
141
     *
142
     * @param Os $os
143
     * @param UserAgent $userAgent
144
     *
145
     * @return bool
146
     */
147 5
    private static function checkWindows(Os $os, UserAgent $userAgent)
148
    {
149 5
        if (stripos($userAgent->getUserAgentString(), 'Windows NT') !== false) {
150 3
            $os->setName($os::WINDOWS);
151
            // Windows version
152 3
            if (preg_match('/Windows NT ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
153 3
                if (isset($matches[1])) {
154 3
                    switch (str_replace('_', '.', $matches[1])) {
155 3
                        case '6.3':
156
                            $os->setVersion('8.1');
157
                            break;
158 3
                        case '6.2':
159 1
                            $os->setVersion('8');
160 1
                            break;
161 3
                        case '6.1':
162 3
                            $os->setVersion('7');
163 3
                            break;
164 1
                        case '6.0':
165
                            $os->setVersion('Vista');
166
                            break;
167 1
                        case '5.2':
168 1
                        case '5.1':
169
                            $os->setVersion('XP');
170
                            break;
171 1
                        case '5.01':
172 1
                        case '5.0':
173
                            $os->setVersion('2000');
174
                            break;
175 1
                        case '4.0':
176
                            $os->setVersion('NT 4.0');
177
                            break;
178 1
                        default:
179 1
                            if ((float)$matches[1] >= 10.0) {
180 1
                                $os->setVersion($matches[1]);
181 1
                            }
182 1
                            break;
183 3
                    }
184 3
                }
185 3
            }
186
187 3
            return true;
188
        } // Windows Me, Windows 98, Windows 95, Windows CE
189 3
        elseif (preg_match(
190 3
            '/(Windows 98; Win 9x 4\.90|Windows 98|Windows 95|Windows CE)/i',
191 3
            $userAgent->getUserAgentString(),
192
            $matches
193 3
        )) {
194
            $os->setName($os::WINDOWS);
195
            switch (strtolower($matches[0])) {
196
                case 'windows 98; win 9x 4.90':
197
                    $os->setVersion('Me');
198
                    break;
199
                case 'windows 98':
200
                    $os->setVersion('98');
201
                    break;
202
                case 'windows 95':
203
                    $os->setVersion('95');
204
                    break;
205
                case 'windows ce':
206
                    $os->setVersion('CE');
207
                    break;
208
            }
209
210
            return true;
211
        }
212
213 3
        return false;
214
    }
215
    
216
    /**
217
     * Determine if the user's operating system is Windows Phone.
218
     *
219
     * @param Os $os
220
     * @param UserAgent $userAgent
221
     *
222
     * @return bool
223
     */
224 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...
225
    {
226 3
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
227 1
            $os->setName($os::WINDOWS_PHONE);
228
            // Windows version
229 1
            if (preg_match('/Windows Phone ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
230 1
                if (isset($matches[1])) {
231 1
                    $os->setVersion((float)$matches[1]);
232 1
                }
233 1
            }
234
    
235 1
            return true;
236
        }
237 3
        return false;
238
    }
239
240
    /**
241
     * Determine if the user's operating system is SymbOS.
242
     *
243
     * @param Os $os
244
     * @param UserAgent $userAgent
245
     *
246
     * @return bool
247
     */
248 5
    private static function checkSymbOS(Os $os, UserAgent $userAgent)
249
    {
250 5
        if (stripos($userAgent->getUserAgentString(), 'SymbOS') !== false) {
251
            $os->setName($os::SYMBOS);
252
253
            return true;
254
        }
255
256 5
        return false;
257
    }
258
259
    /**
260
     * Determine if the user's operating system is Linux.
261
     *
262
     * @param Os $os
263
     * @param UserAgent $userAgent
264
     *
265
     * @return bool
266
     */
267 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...
268
    {
269 3
        if (stripos($userAgent->getUserAgentString(), 'Linux') !== false) {
270 1
            $os->setVersion($os::VERSION_UNKNOWN);
271 1
            $os->setName($os::LINUX);
272
273 1
            return true;
274
        }
275
276 3
        return false;
277
    }
278
279
    /**
280
     * Determine if the user's operating system is Nokia.
281
     *
282
     * @param Os $os
283
     * @param UserAgent $userAgent
284
     *
285
     * @return bool
286
     */
287 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...
288
    {
289 3
        if (stripos($userAgent->getUserAgentString(), 'Nokia') !== false) {
290
            $os->setVersion($os::VERSION_UNKNOWN);
291
            $os->setName($os::NOKIA);
292
            $os->setIsMobile(true);
293
294
            return true;
295
        }
296
297 3
        return false;
298
    }
299
300
    /**
301
     * Determine if the user's operating system is BlackBerry.
302
     *
303
     * @param Os $os
304
     * @param UserAgent $userAgent
305
     *
306
     * @return bool
307
     */
308 3
    private static function checkBlackBerry(Os $os, UserAgent $userAgent)
309
    {
310 3
        if (stripos($userAgent->getUserAgentString(), 'BlackBerry') !== false) {
311 1
            $os->setVersion($os::VERSION_UNKNOWN);
312 1
            $os->setName($os::BLACKBERRY);
313 1
            $os->setIsMobile(true);
314
315 1
            return true;
316 2
        } elseif (stripos($userAgent->getUserAgentString(), 'BB10') !== false) {
317 1
            $aresult = explode('Version/10.', $userAgent->getUserAgentString());
318 1
            if (isset($aresult[1])) {
319 1
                $aversion = explode(' ', $aresult[1]);
320 1
                $os->setVersion('10.' . $aversion[0]);
321 1
            } else {
322
                $os->setVersion('10');
323
            }
324 1
            $os->setName($os::BLACKBERRY);
325 1
            $os->setIsMobile(true);
326
327 1
            return true;
328
        }
329
330 1
        return false;
331
    }
332
333
    /**
334
     * Determine if the user's operating system is Android.
335
     *
336
     * @param Os $os
337
     * @param UserAgent $userAgent
338
     *
339
     * @return bool
340
     */
341 3
    private static function checkAndroid(Os $os, UserAgent $userAgent)
342
    {
343 3
        if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
344
            if (preg_match('/Android ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
345
                if (isset($matches[1])) {
346
                    $os->setVersion($matches[1]);
347
                }
348
            } else {
349
                $os->setVersion($os::VERSION_UNKNOWN);
350
            }
351
            $os->setName($os::ANDROID);
352
            $os->setIsMobile(true);
353
354
            return true;
355
        }
356
357 3
        return false;
358
    }
359
360
    /**
361
     * Determine if the user's operating system is FreeBSD.
362
     *
363
     * @param Os $os
364
     * @param UserAgent $userAgent
365
     *
366
     * @return bool
367
     */
368 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...
369
    {
370 3
        if (stripos($userAgent->getUserAgentString(), 'FreeBSD') !== false) {
371
            $os->setVersion($os::VERSION_UNKNOWN);
372
            $os->setName($os::FREEBSD);
373
374
            return true;
375
        }
376
377 3
        return false;
378
    }
379
380
    /**
381
     * Determine if the user's operating system is OpenBSD.
382
     *
383
     * @param Os $os
384
     * @param UserAgent $userAgent
385
     *
386
     * @return bool
387
     */
388 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...
389
    {
390 3
        if (stripos($userAgent->getUserAgentString(), 'OpenBSD') !== false) {
391
            $os->setVersion($os::VERSION_UNKNOWN);
392
            $os->setName($os::OPENBSD);
393
394
            return true;
395
        }
396
397 3
        return false;
398
    }
399
400
    /**
401
     * Determine if the user's operating system is SunOS.
402
     *
403
     * @param Os $os
404
     * @param UserAgent $userAgent
405
     *
406
     * @return bool
407
     */
408 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...
409
    {
410 3
        if (stripos($userAgent->getUserAgentString(), 'SunOS') !== false) {
411
            $os->setVersion($os::VERSION_UNKNOWN);
412
            $os->setName($os::SUNOS);
413
414
            return true;
415
        }
416
417 3
        return false;
418
    }
419
420
    /**
421
     * Determine if the user's operating system is NetBSD.
422
     *
423
     * @param Os $os
424
     * @param UserAgent $userAgent
425
     *
426
     * @return bool
427
     */
428 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...
429
    {
430 3
        if (stripos($userAgent->getUserAgentString(), 'NetBSD') !== false) {
431
            $os->setVersion($os::VERSION_UNKNOWN);
432
            $os->setName($os::NETBSD);
433
434
            return true;
435
        }
436
437 3
        return false;
438
    }
439
440
    /**
441
     * Determine if the user's operating system is OpenSolaris.
442
     *
443
     * @param Os $os
444
     * @param UserAgent $userAgent
445
     *
446
     * @return bool
447
     */
448 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...
449
    {
450 3
        if (stripos($userAgent->getUserAgentString(), 'OpenSolaris') !== false) {
451
            $os->setVersion($os::VERSION_UNKNOWN);
452
            $os->setName($os::OPENSOLARIS);
453
454
            return true;
455
        }
456
457 3
        return false;
458
    }
459
460
    /**
461
     * Determine if the user's operating system is OS2.
462
     *
463
     * @param Os $os
464
     * @param UserAgent $userAgent
465
     *
466
     * @return bool
467
     */
468 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...
469
    {
470 3
        if (stripos($userAgent->getUserAgentString(), 'OS\/2') !== false) {
471
            $os->setVersion($os::VERSION_UNKNOWN);
472
            $os->setName($os::OS2);
473
474
            return true;
475
        }
476
477 3
        return false;
478
    }
479
480
    /**
481
     * Determine if the user's operating system is BeOS.
482
     *
483
     * @param Os $os
484
     * @param UserAgent $userAgent
485
     *
486
     * @return bool
487
     */
488 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...
489
    {
490 3
        if (stripos($userAgent->getUserAgentString(), 'BeOS') !== false) {
491
            $os->setVersion($os::VERSION_UNKNOWN);
492
            $os->setName($os::BEOS);
493
494
            return true;
495
        }
496
497 3
        return false;
498
    }
499
}
500