Completed
Push — master ( 7db06b...51fae5 )
by Gabriel
03:38
created

OsDetector::checkNokia()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
ccs 3
cts 7
cp 0.4286
rs 9.4286
cc 2
eloc 7
nc 2
nop 2
crap 2.7462
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
            // iOS before OS X
25 9
            self::checkIOS($os, $userAgent) ||
26 7
            self::checkOSX($os, $userAgent) ||
27 5
            self::checkSymbOS($os, $userAgent) ||
28 5
            self::checkWindows($os, $userAgent) ||
29 2
            self::checkWindowsPhone($os, $userAgent) ||
30 2
            self::checkFreeBSD($os, $userAgent) ||
31 2
            self::checkOpenBSD($os, $userAgent) ||
32 2
            self::checkNetBSD($os, $userAgent) ||
33 2
            self::checkOpenSolaris($os, $userAgent) ||
34 2
            self::checkSunOS($os, $userAgent) ||
35 2
            self::checkOS2($os, $userAgent) ||
36 2
            self::checkBeOS($os, $userAgent) ||
37
            // Android before Linux
38 2
            self::checkAndroid($os, $userAgent) ||
39 2
            self::checkLinux($os, $userAgent) ||
40 2
            self::checkNokia($os, $userAgent) ||
41 2
            self::checkBlackBerry($os, $userAgent)
42 9
        );
43
    }
44
45
    /**
46
     * Determine if the user's browser is on a mobile device.
47
     *
48
     * @param Os $os
49
     * @param UserAgent $userAgent
50
     *
51
     * @return bool
52
     */
53 9
    public static function checkMobileBrowsers(Os $os, UserAgent $userAgent)
54
    {
55
        // Check for Opera Mini
56 9
        if (stripos($userAgent->getUserAgentString(), 'opera mini') !== false) {
57
            $os->setIsMobile(true);
58
        } // Set is mobile for Pocket IE
59 9
        elseif (stripos($userAgent->getUserAgentString(), 'mspie') !== false || stripos($userAgent->getUserAgentString(), 'pocket') !== false) {
60
            $os->setIsMobile(true);
61
        }
62 9
    }
63
64
    /**
65
     * Determine if the user's operating system is iOS.
66
     *
67
     * @param Os $os
68
     * @param UserAgent $userAgent
69
     *
70
     * @return bool
71
     */
72 9
    private static function checkIOS(Os $os, UserAgent $userAgent)
73
    {
74 9
        if (stripos($userAgent->getUserAgentString(), 'CPU OS') !== false || stripos($userAgent->getUserAgentString(), 'iPhone OS') !== false && stripos($userAgent->getUserAgentString(), 'OS X')) {
75 3
            $os->setName($os::IOS);
76 3
            if (preg_match('/CPU( iPhone)? OS ([\d_]*)/i', $userAgent->getUserAgentString(), $matches)) {
77 3
                $os->setVersion(str_replace('_', '.', $matches[2]));
78 3
            }
79 3
            $os->setIsMobile(true);
80
81 3
            return true;
82
        }
83
84 7
        return false;
85
    }
86
87
    /**
88
     * Determine if the user's operating system is OS X.
89
     *
90
     * @param Os $os
91
     * @param UserAgent $userAgent
92
     *
93
     * @return bool
94
     */
95 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...
96
    {
97 7
        if (stripos($userAgent->getUserAgentString(), 'OS X') !== false) {
98 3
            $os->setName($os::OSX);
99 3
            if (preg_match('/OS X ([\d\._]*)/i', $userAgent->getUserAgentString(), $matches)) {
100 3
                if (isset($matches[1])) {
101 3
                    $os->setVersion(str_replace('_', '.', $matches[1]));
102 3
                }
103 3
            }
104
105 3
            return true;
106
        }
107
108 5
        return false;
109
    }
110
111
    /**
112
     * Determine if the user's operating system is Windows.
113
     *
114
     * @param Os $os
115
     * @param UserAgent $userAgent
116
     *
117
     * @return bool
118
     */
119 5
    private static function checkWindows(Os $os, UserAgent $userAgent)
120
    {
121 5
        if (stripos($userAgent->getUserAgentString(), 'Windows NT') !== false) {
122 3
            $os->setName($os::WINDOWS);
123
            // Windows version
124 3
            if (preg_match('/Windows NT ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
125 3
                if (isset($matches[1])) {
126 3
                    switch (str_replace('_', '.', $matches[1])) {
127 3
                        case '6.3':
128
                            $os->setVersion('8.1');
129
                            break;
130 3
                        case '6.2':
131
                            $os->setVersion('8');
132
                            break;
133 3
                        case '6.1':
134 3
                            $os->setVersion('7');
135 3
                            break;
136 1
                        case '6.0':
137
                            $os->setVersion('Vista');
138
                            break;
139 1
                        case '5.2':
140 1
                        case '5.1':
141
                            $os->setVersion('XP');
142
                            break;
143 1
                        case '5.01':
144 1
                        case '5.0':
145
                            $os->setVersion('2000');
146
                            break;
147 1
                        case '4.0':
148
                            $os->setVersion('NT 4.0');
149
                            break;
150 1
                        default:
151 1
                            if ((float)$matches[1] >= 10.0) {
152 1
                                $os->setVersion((float)$matches[1]);
153 1
                            }
154 1
                            break;
155 3
                    }
156 3
                }
157 3
            }
158
159 3
            return true;
160
        } // Windows Me, Windows 98, Windows 95, Windows CE
161 2
        elseif (preg_match('/(Windows 98; Win 9x 4\.90|Windows 98|Windows 95|Windows CE)/i', $userAgent->getUserAgentString(), $matches)) {
162
            $os->setName($os::WINDOWS);
163
            switch (strtolower($matches[0])) {
164
                case 'windows 98; win 9x 4.90':
165
                    $os->setVersion('Me');
166
                    break;
167
                case 'windows 98':
168
                    $os->setVersion('98');
169
                    break;
170
                case 'windows 95':
171
                    $os->setVersion('95');
172
                    break;
173
                case 'windows ce':
174
                    $os->setVersion('CE');
175
                    break;
176
            }
177
178
            return true;
179
        }
180
181 2
        return false;
182
    }
183
    
184
    /**
185
     * Determine if the user's operating system is Windows Phone.
186
     *
187
     * @param Os $os
188
     * @param UserAgent $userAgent
189
     *
190
     * @return bool
191
     */
192 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...
193
    {
194 2
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
195
            $os->setName($os::WINDOWS_PHONE);
196
            // Windows version
197
            if (preg_match('/Windows Phone ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
198
                if (isset($matches[1])) {
199
                    $os->setVersion((float)$matches[1]);
200
                }
201
            }
202
    
203
            return true;
204
        }
205 2
        return false;
206
    }
207
208
    /**
209
     * Determine if the user's operating system is SymbOS.
210
     *
211
     * @param Os $os
212
     * @param UserAgent $userAgent
213
     *
214
     * @return bool
215
     */
216 5
    private static function checkSymbOS(Os $os, UserAgent $userAgent)
217
    {
218 5
        if (stripos($userAgent->getUserAgentString(), 'SymbOS') !== false) {
219
            $os->setName($os::SYMBOS);
220
221
            return true;
222
        }
223
224 5
        return false;
225
    }
226
227
    /**
228
     * Determine if the user's operating system is Linux.
229
     *
230
     * @param Os $os
231
     * @param UserAgent $userAgent
232
     *
233
     * @return bool
234
     */
235 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...
236
    {
237 2
        if (stripos($userAgent->getUserAgentString(), 'Linux') !== false) {
238
            $os->setVersion($os::VERSION_UNKNOWN);
239
            $os->setName($os::LINUX);
240
241
            return true;
242
        }
243
244 2
        return false;
245
    }
246
247
    /**
248
     * Determine if the user's operating system is Nokia.
249
     *
250
     * @param Os $os
251
     * @param UserAgent $userAgent
252
     *
253
     * @return bool
254
     */
255 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...
256
    {
257 2
        if (stripos($userAgent->getUserAgentString(), 'Nokia') !== false) {
258
            $os->setVersion($os::VERSION_UNKNOWN);
259
            $os->setName($os::NOKIA);
260
            $os->setIsMobile(true);
261
262
            return true;
263
        }
264
265 2
        return false;
266
    }
267
268
    /**
269
     * Determine if the user's operating system is BlackBerry.
270
     *
271
     * @param Os $os
272
     * @param UserAgent $userAgent
273
     *
274
     * @return bool
275
     */
276 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...
277
    {
278 2
        if (stripos($userAgent->getUserAgentString(), 'BlackBerry') !== false) {
279 1
            $os->setVersion($os::VERSION_UNKNOWN);
280 1
            $os->setName($os::BLACKBERRY);
281 1
            $os->setIsMobile(true);
282
283 1
            return true;
284
        }
285
286 1
        return false;
287
    }
288
289
    /**
290
     * Determine if the user's operating system is Android.
291
     *
292
     * @param Os $os
293
     * @param UserAgent $userAgent
294
     *
295
     * @return bool
296
     */
297 2
    private static function checkAndroid(Os $os, UserAgent $userAgent)
298
    {
299 2
        if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
300
            if (preg_match('/Android ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
301
                if (isset($matches[1])) {
302
                    $os->setVersion($matches[1]);
303
                }
304
            } else {
305
                $os->setVersion($os::VERSION_UNKNOWN);
306
            }
307
            $os->setName($os::ANDROID);
308
            $os->setIsMobile(true);
309
310
            return true;
311
        }
312
313 2
        return false;
314
    }
315
316
    /**
317
     * Determine if the user's operating system is FreeBSD.
318
     *
319
     * @param Os $os
320
     * @param UserAgent $userAgent
321
     *
322
     * @return bool
323
     */
324 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...
325
    {
326 2
        if (stripos($userAgent->getUserAgentString(), 'FreeBSD') !== false) {
327
            $os->setVersion($os::VERSION_UNKNOWN);
328
            $os->setName($os::FREEBSD);
329
330
            return true;
331
        }
332
333 2
        return false;
334
    }
335
336
    /**
337
     * Determine if the user's operating system is OpenBSD.
338
     *
339
     * @param Os $os
340
     * @param UserAgent $userAgent
341
     *
342
     * @return bool
343
     */
344 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...
345
    {
346 2
        if (stripos($userAgent->getUserAgentString(), 'OpenBSD') !== false) {
347
            $os->setVersion($os::VERSION_UNKNOWN);
348
            $os->setName($os::OPENBSD);
349
350
            return true;
351
        }
352
353 2
        return false;
354
    }
355
356
    /**
357
     * Determine if the user's operating system is SunOS.
358
     *
359
     * @param Os $os
360
     * @param UserAgent $userAgent
361
     *
362
     * @return bool
363
     */
364 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...
365
    {
366 2
        if (stripos($userAgent->getUserAgentString(), 'SunOS') !== false) {
367
            $os->setVersion($os::VERSION_UNKNOWN);
368
            $os->setName($os::SUNOS);
369
370
            return true;
371
        }
372
373 2
        return false;
374
    }
375
376
    /**
377
     * Determine if the user's operating system is NetBSD.
378
     *
379
     * @param Os $os
380
     * @param UserAgent $userAgent
381
     *
382
     * @return bool
383
     */
384 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...
385
    {
386 2
        if (stripos($userAgent->getUserAgentString(), 'NetBSD') !== false) {
387
            $os->setVersion($os::VERSION_UNKNOWN);
388
            $os->setName($os::NETBSD);
389
390
            return true;
391
        }
392
393 2
        return false;
394
    }
395
396
    /**
397
     * Determine if the user's operating system is OpenSolaris.
398
     *
399
     * @param Os $os
400
     * @param UserAgent $userAgent
401
     *
402
     * @return bool
403
     */
404 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...
405
    {
406 2
        if (stripos($userAgent->getUserAgentString(), 'OpenSolaris') !== false) {
407
            $os->setVersion($os::VERSION_UNKNOWN);
408
            $os->setName($os::OPENSOLARIS);
409
410
            return true;
411
        }
412
413 2
        return false;
414
    }
415
416
    /**
417
     * Determine if the user's operating system is OS2.
418
     *
419
     * @param Os $os
420
     * @param UserAgent $userAgent
421
     *
422
     * @return bool
423
     */
424 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...
425
    {
426 2
        if (stripos($userAgent->getUserAgentString(), 'OS\/2') !== false) {
427
            $os->setVersion($os::VERSION_UNKNOWN);
428
            $os->setName($os::OS2);
429
430
            return true;
431
        }
432
433 2
        return false;
434
    }
435
436
    /**
437
     * Determine if the user's operating system is BeOS.
438
     *
439
     * @param Os $os
440
     * @param UserAgent $userAgent
441
     *
442
     * @return bool
443
     */
444 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...
445
    {
446 2
        if (stripos($userAgent->getUserAgentString(), 'BeOS') !== false) {
447
            $os->setVersion($os::VERSION_UNKNOWN);
448
            $os->setName($os::BEOS);
449
450
            return true;
451
        }
452
453 2
        return false;
454
    }
455
}
456