DeviceDetectorService   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 140
rs 10
wmc 27

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getBrowser() 0 21 10
A getBrowserVersion() 0 20 2
A getPlatformVersion() 0 5 2
A getDeviceType() 0 15 4
B getPlatform() 0 25 8
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Services;
6
7
use Detection\Exception\MobileDetectException;
8
use Detection\MobileDetect;
9
10
class DeviceDetectorService
11
{
12
    protected MobileDetect $detect;
13
14
    public function __construct()
15
    {
16
        $this->detect = new MobileDetect();
17
    }
18
19
    /**
20
     * Get the platform.
21
     *
22
     * @param string|null $userAgent
23
     *
24
     * @return string
25
     */
26
    public function getPlatform(?string $userAgent = null): string
27
    {
28
        if ($userAgent) {
29
            $this->detect->setUserAgent($userAgent);
30
        }
31
32
        if ($this->detect->isiOS()) {
33
            return 'iOS';
34
        }
35
        if ($this->detect->isAndroidOS()) {
36
            return 'Android';
37
        }
38
        if ($this->detect->isWindowsMobileOS()) {
39
            return 'Windows Phone';
40
        }
41
        if (mb_stripos($userAgent, 'Windows NT') !== false) {
0 ignored issues
show
Bug introduced by
It seems like $userAgent can also be of type null; however, parameter $haystack of mb_stripos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        if (mb_stripos(/** @scrutinizer ignore-type */ $userAgent, 'Windows NT') !== false) {
Loading history...
42
            return 'Windows';
43
        }
44
        if (mb_stripos($userAgent, 'Macintosh') !== false) {
45
            return 'macOS';
46
        }
47
        if (mb_stripos($userAgent, 'Linux') !== false) {
48
            return 'Linux';
49
        }
50
        return 'Unknown OS';
51
    }
52
53
    /**
54
     * Get the platform version.
55
     *
56
     * @param string $userAgent
57
     *
58
     * @return string
59
     */
60
    public function getPlatformVersion(string $userAgent): string
61
    {
62
        preg_match('/(Windows NT|Android|CPU (iPhone )?OS|Mac OS X|Linux) ([0-9_\.]+)/i', $userAgent, $matches);
63
64
        return isset($matches[3]) ? str_replace('_', '.', $matches[3]) : 'Unknown Version';
65
    }
66
67
    /**
68
     * Get the browser.
69
     *
70
     * @param string $userAgent
71
     *
72
     * @return string
73
     */
74
    public function getBrowser(string $userAgent): string
75
    {
76
        if (mb_stripos($userAgent, 'Chrome') !== false) {
77
            return 'Chrome';
78
        }
79
        if (mb_stripos($userAgent, 'Firefox') !== false) {
80
            return 'Firefox';
81
        }
82
        if (mb_stripos($userAgent, 'Safari') !== false && mb_stripos($userAgent, 'Chrome') === false) {
83
            return 'Safari';
84
        }
85
        if (mb_stripos($userAgent, 'Edge') !== false) {
86
            return 'Edge';
87
        }
88
        if (mb_stripos($userAgent, 'OPR') !== false || mb_stripos($userAgent, 'Opera') !== false) {
89
            return 'Opera';
90
        }
91
        if (mb_stripos($userAgent, 'MSIE') !== false || mb_stripos($userAgent, 'Trident') !== false) {
92
            return 'Internet Explorer';
93
        }
94
        return 'Unknown Browser';
95
    }
96
97
    /**
98
     * Get the version of the browser.
99
     *
100
     * @param string $userAgent
101
     *
102
     * @return string
103
     */
104
    public function getBrowserVersion(string $userAgent): string
105
    {
106
        $browser = $this->getBrowser($userAgent);
107
        $version = 'Unknown Version';
108
109
        $patterns = [
110
            'Chrome' => '/Chrome\/([0-9\.]+)/',
111
            'Firefox' => '/Firefox\/([0-9\.]+)/',
112
            'Safari' => '/Version\/([0-9\.]+)/',
113
            'Edge' => '/Edg\/([0-9\.]+)/',
114
            'Opera' => '/(Opera|OPR)\/([0-9\.]+)/',
115
            'Internet Explorer' => '/(MSIE |rv:)([0-9\.]+)/',
116
        ];
117
118
        if (isset($patterns[$browser])) {
119
            preg_match($patterns[$browser], $userAgent, $matches);
120
            $version = $matches[1] ?? $matches[2] ?? 'Unknown Version';
121
        }
122
123
        return $version;
124
    }
125
126
    /**
127
     * Return the type of device (desktop, phone, tablet)
128
     *
129
     * @param string|null $userAgent
130
     *
131
     * @return string
132
     *
133
     * @throws MobileDetectException
134
     */
135
    public function getDeviceType(?string $userAgent = null): string
136
    {
137
        if ($userAgent) {
138
            $this->detect->setUserAgent($userAgent);
139
        }
140
141
        if ($this->detect->isTablet()) {
142
            return 'tablet';
143
        }
144
145
        if ($this->detect->isMobile()) {
146
            return 'phone';
147
        }
148
149
        return 'desktop';
150
    }
151
}
152