Completed
Push — master ( 7e3ce6...84d414 )
by Gabriel
02:44
created

OsDetector::checkChromeOs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3
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 2
            self::checkWindowsPhone($os, $userAgent) ||
32 2
            self::checkFreeBSD($os, $userAgent) ||
33 2
            self::checkOpenBSD($os, $userAgent) ||
34 2
            self::checkNetBSD($os, $userAgent) ||
35 2
            self::checkOpenSolaris($os, $userAgent) ||
36 2
            self::checkSunOS($os, $userAgent) ||
37 2
            self::checkOS2($os, $userAgent) ||
38 2
            self::checkBeOS($os, $userAgent) ||
39
            // Android before Linux
40 2
            self::checkAndroid($os, $userAgent) ||
41 2
            self::checkLinux($os, $userAgent) ||
42 2
            self::checkNokia($os, $userAgent) ||
43 2
            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
                            $os->setVersion('8');
158
                            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 2
        elseif (preg_match(
188 2
            '/(Windows 98; Win 9x 4\.90|Windows 98|Windows 95|Windows CE)/i',
189 2
            $userAgent->getUserAgentString(),
190
            $matches
191 2
        )) {
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 2
        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 2 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 2
        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 2
        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 2 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 2
        if (stripos($userAgent->getUserAgentString(), 'Linux') !== false) {
268
            $os->setVersion($os::VERSION_UNKNOWN);
269
            $os->setName($os::LINUX);
270
271
            return true;
272
        }
273
274 2
        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 2 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 2
        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 2
        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 2 View Code Duplication
    private static function checkBlackBerry(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...
307
    {
308 2
        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
        }
315
316 1
        return false;
317
    }
318
319
    /**
320
     * Determine if the user's operating system is Android.
321
     *
322
     * @param Os $os
323
     * @param UserAgent $userAgent
324
     *
325
     * @return bool
326
     */
327 2
    private static function checkAndroid(Os $os, UserAgent $userAgent)
328
    {
329 2
        if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
330
            if (preg_match('/Android ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
331
                if (isset($matches[1])) {
332
                    $os->setVersion($matches[1]);
333
                }
334
            } else {
335
                $os->setVersion($os::VERSION_UNKNOWN);
336
            }
337
            $os->setName($os::ANDROID);
338
            $os->setIsMobile(true);
339
340
            return true;
341
        }
342
343 2
        return false;
344
    }
345
346
    /**
347
     * Determine if the user's operating system is FreeBSD.
348
     *
349
     * @param Os $os
350
     * @param UserAgent $userAgent
351
     *
352
     * @return bool
353
     */
354 2 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...
355
    {
356 2
        if (stripos($userAgent->getUserAgentString(), 'FreeBSD') !== false) {
357
            $os->setVersion($os::VERSION_UNKNOWN);
358
            $os->setName($os::FREEBSD);
359
360
            return true;
361
        }
362
363 2
        return false;
364
    }
365
366
    /**
367
     * Determine if the user's operating system is OpenBSD.
368
     *
369
     * @param Os $os
370
     * @param UserAgent $userAgent
371
     *
372
     * @return bool
373
     */
374 2 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...
375
    {
376 2
        if (stripos($userAgent->getUserAgentString(), 'OpenBSD') !== false) {
377
            $os->setVersion($os::VERSION_UNKNOWN);
378
            $os->setName($os::OPENBSD);
379
380
            return true;
381
        }
382
383 2
        return false;
384
    }
385
386
    /**
387
     * Determine if the user's operating system is SunOS.
388
     *
389
     * @param Os $os
390
     * @param UserAgent $userAgent
391
     *
392
     * @return bool
393
     */
394 2 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...
395
    {
396 2
        if (stripos($userAgent->getUserAgentString(), 'SunOS') !== false) {
397
            $os->setVersion($os::VERSION_UNKNOWN);
398
            $os->setName($os::SUNOS);
399
400
            return true;
401
        }
402
403 2
        return false;
404
    }
405
406
    /**
407
     * Determine if the user's operating system is NetBSD.
408
     *
409
     * @param Os $os
410
     * @param UserAgent $userAgent
411
     *
412
     * @return bool
413
     */
414 2 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...
415
    {
416 2
        if (stripos($userAgent->getUserAgentString(), 'NetBSD') !== false) {
417
            $os->setVersion($os::VERSION_UNKNOWN);
418
            $os->setName($os::NETBSD);
419
420
            return true;
421
        }
422
423 2
        return false;
424
    }
425
426
    /**
427
     * Determine if the user's operating system is OpenSolaris.
428
     *
429
     * @param Os $os
430
     * @param UserAgent $userAgent
431
     *
432
     * @return bool
433
     */
434 2 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...
435
    {
436 2
        if (stripos($userAgent->getUserAgentString(), 'OpenSolaris') !== false) {
437
            $os->setVersion($os::VERSION_UNKNOWN);
438
            $os->setName($os::OPENSOLARIS);
439
440
            return true;
441
        }
442
443 2
        return false;
444
    }
445
446
    /**
447
     * Determine if the user's operating system is OS2.
448
     *
449
     * @param Os $os
450
     * @param UserAgent $userAgent
451
     *
452
     * @return bool
453
     */
454 2 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...
455
    {
456 2
        if (stripos($userAgent->getUserAgentString(), 'OS\/2') !== false) {
457
            $os->setVersion($os::VERSION_UNKNOWN);
458
            $os->setName($os::OS2);
459
460
            return true;
461
        }
462
463 2
        return false;
464
    }
465
466
    /**
467
     * Determine if the user's operating system is BeOS.
468
     *
469
     * @param Os $os
470
     * @param UserAgent $userAgent
471
     *
472
     * @return bool
473
     */
474 2 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...
475
    {
476 2
        if (stripos($userAgent->getUserAgentString(), 'BeOS') !== false) {
477
            $os->setVersion($os::VERSION_UNKNOWN);
478
            $os->setName($os::BEOS);
479
480
            return true;
481
        }
482
483 2
        return false;
484
    }
485
}
486