1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\BrowserDetector; |
4
|
|
|
|
5
|
|
|
class DeviceDetector implements DetectorInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Determine the user's device. |
9
|
|
|
* |
10
|
|
|
* @param Device $device |
11
|
|
|
* @param UserAgent $userAgent |
12
|
|
|
* @return bool |
13
|
|
|
*/ |
14
|
2 |
|
public static function detect(Device $device, UserAgent $userAgent) |
15
|
|
|
{ |
16
|
2 |
|
$device->setName($device::UNKNOWN); |
17
|
|
|
|
18
|
|
|
return ( |
19
|
2 |
|
self::checkIpad($device, $userAgent) || |
20
|
2 |
|
self::checkIphone($device, $userAgent) || |
21
|
2 |
|
self::checkWindowsPhone($device, $userAgent) |
22
|
2 |
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Determine if the device is iPad. |
27
|
|
|
* |
28
|
|
|
* @param Device $device |
29
|
|
|
* @param UserAgent $userAgent |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
2 |
View Code Duplication |
private static function checkIpad(Device $device, UserAgent $userAgent) |
|
|
|
|
33
|
|
|
{ |
34
|
2 |
|
if (stripos($userAgent->getUserAgentString(), 'ipad') !== false) { |
35
|
1 |
|
$device->setName(Device::IPAD); |
36
|
1 |
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine if the device is iPhone. |
44
|
|
|
* |
45
|
|
|
* @param Device $device |
46
|
|
|
* @param UserAgent $userAgent |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
2 |
View Code Duplication |
private static function checkIphone(Device $device, UserAgent $userAgent) |
|
|
|
|
50
|
|
|
{ |
51
|
2 |
|
if (stripos($userAgent->getUserAgentString(), 'iphone;') !== false) { |
52
|
1 |
|
$device->setName(Device::IPHONE); |
53
|
1 |
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Determine if the device is Windows Phone. |
61
|
|
|
* |
62
|
|
|
* @param Device $device |
63
|
|
|
* @param UserAgent $userAgent |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
2 |
View Code Duplication |
private static function checkWindowsPhone(Device $device, UserAgent $userAgent) |
|
|
|
|
67
|
|
|
{ |
68
|
2 |
|
if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) { |
69
|
1 |
|
if (preg_match('/Microsoft; (Lumia [^)]*)\)/', $userAgent->getUserAgentString(), $matches)) { |
70
|
1 |
|
$device->setName($matches[1]); |
71
|
1 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$device->setName($device::WINDOWS_PHONE); |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
2 |
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.