Completed
Push — master ( b686a6...8f2f13 )
by Gabriel
04:01
created

OsDetector::checkOS2()   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 8
            self::checkOSX($os, $userAgent) ||
29 6
            self::checkSymbOS($os, $userAgent) ||
30 6
            self::checkWindows($os, $userAgent) ||
31 4
            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 8
        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 8 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 8
        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 6
        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 6
    private static function checkWindows(Os $os, UserAgent $userAgent)
148
    {
149 6
        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 4
        elseif (preg_match(
190 4
            '/(Windows 98; Win 9x 4\.90|Windows 98|Windows 95|Windows CE)/i',
191 4
            $userAgent->getUserAgentString(),
192
            $matches
193 4
        )) {
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 4
        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 4 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 4
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
227 2
            $os->setIsMobile(true);
228 2
            $os->setName($os::WINDOWS_PHONE);
229
            // Windows version
230 2
            if (preg_match('/Windows Phone ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
231 2
                if (isset($matches[1])) {
232 2
                    $os->setVersion((float)$matches[1]);
233 2
                }
234 2
            }
235
    
236 2
            return true;
237
        }
238 3
        return false;
239
    }
240
241
    /**
242
     * Determine if the user's operating system is SymbOS.
243
     *
244
     * @param Os $os
245
     * @param UserAgent $userAgent
246
     *
247
     * @return bool
248
     */
249 6
    private static function checkSymbOS(Os $os, UserAgent $userAgent)
250
    {
251 6
        if (stripos($userAgent->getUserAgentString(), 'SymbOS') !== false) {
252
            $os->setName($os::SYMBOS);
253
254
            return true;
255
        }
256
257 6
        return false;
258
    }
259
260
    /**
261
     * Determine if the user's operating system is Linux.
262
     *
263
     * @param Os $os
264
     * @param UserAgent $userAgent
265
     *
266
     * @return bool
267
     */
268 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...
269
    {
270 3
        if (stripos($userAgent->getUserAgentString(), 'Linux') !== false) {
271 1
            $os->setVersion($os::VERSION_UNKNOWN);
272 1
            $os->setName($os::LINUX);
273
274 1
            return true;
275
        }
276
277 3
        return false;
278
    }
279
280
    /**
281
     * Determine if the user's operating system is Nokia.
282
     *
283
     * @param Os $os
284
     * @param UserAgent $userAgent
285
     *
286
     * @return bool
287
     */
288 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...
289
    {
290 3
        if (stripos($userAgent->getUserAgentString(), 'Nokia') !== false) {
291
            $os->setVersion($os::VERSION_UNKNOWN);
292
            $os->setName($os::NOKIA);
293
            $os->setIsMobile(true);
294
295
            return true;
296
        }
297
298 3
        return false;
299
    }
300
301
    /**
302
     * Determine if the user's operating system is BlackBerry.
303
     *
304
     * @param Os $os
305
     * @param UserAgent $userAgent
306
     *
307
     * @return bool
308
     */
309 3
    private static function checkBlackBerry(Os $os, UserAgent $userAgent)
310
    {
311 3
        if (stripos($userAgent->getUserAgentString(), 'BlackBerry') !== false) {
312 2
            if (stripos($userAgent->getUserAgentString(), 'Version/') !== false) {
313 2
                $aresult = explode('Version/', $userAgent->getUserAgentString());
314 2
                if (isset($aresult[1])) {
315 2
                    $aversion = explode(' ', $aresult[1]);
316 2
                    $os->setVersion($aversion[0]);
317 2
                }
318 2
            } else {
319
                $os->setVersion($os::VERSION_UNKNOWN);
320
            }
321 2
            $os->setName($os::BLACKBERRY);
322 2
            $os->setIsMobile(true);
323
324 2
            return true;
325 2
        } elseif (stripos($userAgent->getUserAgentString(), 'BB10') !== false) {
326 1
            $aresult = explode('Version/10.', $userAgent->getUserAgentString());
327 1
            if (isset($aresult[1])) {
328 1
                $aversion = explode(' ', $aresult[1]);
329 1
                $os->setVersion('10.' . $aversion[0]);
330 1
            } else {
331
                $os->setVersion('10');
332
            }
333 1
            $os->setName($os::BLACKBERRY);
334 1
            $os->setIsMobile(true);
335
336 1
            return true;
337
        }
338
339 1
        return false;
340
    }
341
342
    /**
343
     * Determine if the user's operating system is Android.
344
     *
345
     * @param Os $os
346
     * @param UserAgent $userAgent
347
     *
348
     * @return bool
349
     */
350 3
    private static function checkAndroid(Os $os, UserAgent $userAgent)
351
    {
352 3
        if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
353
            if (preg_match('/Android ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
354
                if (isset($matches[1])) {
355
                    $os->setVersion($matches[1]);
356
                }
357
            } else {
358
                $os->setVersion($os::VERSION_UNKNOWN);
359
            }
360
            $os->setName($os::ANDROID);
361
            $os->setIsMobile(true);
362
363
            return true;
364
        }
365
366 3
        return false;
367
    }
368
369
    /**
370
     * Determine if the user's operating system is FreeBSD.
371
     *
372
     * @param Os $os
373
     * @param UserAgent $userAgent
374
     *
375
     * @return bool
376
     */
377 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...
378
    {
379 3
        if (stripos($userAgent->getUserAgentString(), 'FreeBSD') !== false) {
380
            $os->setVersion($os::VERSION_UNKNOWN);
381
            $os->setName($os::FREEBSD);
382
383
            return true;
384
        }
385
386 3
        return false;
387
    }
388
389
    /**
390
     * Determine if the user's operating system is OpenBSD.
391
     *
392
     * @param Os $os
393
     * @param UserAgent $userAgent
394
     *
395
     * @return bool
396
     */
397 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...
398
    {
399 3
        if (stripos($userAgent->getUserAgentString(), 'OpenBSD') !== false) {
400
            $os->setVersion($os::VERSION_UNKNOWN);
401
            $os->setName($os::OPENBSD);
402
403
            return true;
404
        }
405
406 3
        return false;
407
    }
408
409
    /**
410
     * Determine if the user's operating system is SunOS.
411
     *
412
     * @param Os $os
413
     * @param UserAgent $userAgent
414
     *
415
     * @return bool
416
     */
417 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...
418
    {
419 3
        if (stripos($userAgent->getUserAgentString(), 'SunOS') !== false) {
420
            $os->setVersion($os::VERSION_UNKNOWN);
421
            $os->setName($os::SUNOS);
422
423
            return true;
424
        }
425
426 3
        return false;
427
    }
428
429
    /**
430
     * Determine if the user's operating system is NetBSD.
431
     *
432
     * @param Os $os
433
     * @param UserAgent $userAgent
434
     *
435
     * @return bool
436
     */
437 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...
438
    {
439 3
        if (stripos($userAgent->getUserAgentString(), 'NetBSD') !== false) {
440
            $os->setVersion($os::VERSION_UNKNOWN);
441
            $os->setName($os::NETBSD);
442
443
            return true;
444
        }
445
446 3
        return false;
447
    }
448
449
    /**
450
     * Determine if the user's operating system is OpenSolaris.
451
     *
452
     * @param Os $os
453
     * @param UserAgent $userAgent
454
     *
455
     * @return bool
456
     */
457 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...
458
    {
459 3
        if (stripos($userAgent->getUserAgentString(), 'OpenSolaris') !== false) {
460
            $os->setVersion($os::VERSION_UNKNOWN);
461
            $os->setName($os::OPENSOLARIS);
462
463
            return true;
464
        }
465
466 3
        return false;
467
    }
468
469
    /**
470
     * Determine if the user's operating system is OS2.
471
     *
472
     * @param Os $os
473
     * @param UserAgent $userAgent
474
     *
475
     * @return bool
476
     */
477 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...
478
    {
479 3
        if (stripos($userAgent->getUserAgentString(), 'OS\/2') !== false) {
480
            $os->setVersion($os::VERSION_UNKNOWN);
481
            $os->setName($os::OS2);
482
483
            return true;
484
        }
485
486 3
        return false;
487
    }
488
489
    /**
490
     * Determine if the user's operating system is BeOS.
491
     *
492
     * @param Os $os
493
     * @param UserAgent $userAgent
494
     *
495
     * @return bool
496
     */
497 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...
498
    {
499 3
        if (stripos($userAgent->getUserAgentString(), 'BeOS') !== false) {
500
            $os->setVersion($os::VERSION_UNKNOWN);
501
            $os->setName($os::BEOS);
502
503
            return true;
504
        }
505
506 3
        return false;
507
    }
508
}
509