Passed
Pull Request — master (#19)
by Jonathan
07:38
created

DetectsChromeVersion::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\DuskUpdater;
4
5
use Illuminate\Support\Str;
6
use Symfony\Component\Process\Process;
7
8
trait DetectsChromeVersion
9
{
10
    /**
11
     * The default commands to detect the installed Chrome/Chromium version.
12
     *
13
     * @var array
14
     */
15
    protected static $platforms = [
16
        'linux' => [
17
            'slug' => 'linux64',
18
            'commands' => [
19
                '/usr/bin/google-chrome --version',
20
                '/usr/bin/chromium-browser --version',
21
                '/usr/bin/chromium --version',
22
                '/usr/bin/google-chrome-stable --version',
23
            ],
24
        ],
25
        'mac' => [
26
            'slug' => 'mac-x64',
27
            'commands' => [
28
                '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version',
29
                '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
30
            ],
31
        ],
32
        'mac-intel' => [
33
            'slug' => 'mac-x64',
34 3
            'commands' => [
35
                '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version',
36 3
                '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
37
            ],
38 3
        ],
39 2
        'mac-arm' => [
40
            'slug' => 'mac-arm64',
41
            'commands' => [
42
                '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version',
43
                '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
44
            ],
45 2
        ],
46
        'win' => [
47 1
            'slug' => 'win32',
48
            'commands' => [
49
                'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version',
50 3
            ],
51 3
        ],
52
    ];
53 3
54
    /**
55 3
     * Detect the installed Chrome/Chromium version.
56
     *
57 3
     * @param string $os
58 1
     * @return int|bool
59
     */
60
    protected function chromeVersion($os)
61 2
    {
62 2
        $path = $this->option('detect');
0 ignored issues
show
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-call */ 
63
        $path = $this->option('detect');
Loading history...
63
64
        if ($path) {
65 2
            if ($os === 'win') {
66
                $this->error('Chrome version cannot be detected in custom installation path on Windows.');
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

66
                $this->/** @scrutinizer ignore-call */ 
67
                       error('Chrome version cannot be detected in custom installation path on Windows.');
Loading history...
67
68 1
                return false;
69
            }
70 1
71
            $commands = [$path.' --version'];
72
        } else {
73
            $commands = static::$platforms[$os]['commands'];
74
        }
75
76
        foreach ($commands as $command) {
77
            $process = Process::fromShellCommandline($command);
78
79
            $process->run();
80
81
            preg_match('/(\d+)(\.\d+){3}/', $process->getOutput(), $matches);
82
83
            if (!isset($matches[1])) {
84
                continue;
85
            }
86
87
            $this->comment(
0 ignored issues
show
Bug introduced by
It seems like comment() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

87
            $this->/** @scrutinizer ignore-call */ 
88
                   comment(
Loading history...
88
                sprintf('Chrome version %s detected.', $matches[0])
89
            );
90
91
            return (int) $matches[1];
92
        }
93
94
        $this->error('Chrome version could not be detected. Please submit an issue: https://github.com/staudenmeir/dusk-updater');
95
96
        return false;
97
    }
98
99
    /**
100
     * Resolve the ChromeDriver slug for the given operating system.
101
     *
102
     * @param string $operatingSystem
103
     * @param string|null $version
104
     * @return string
105
     */
106
    public static function chromeDriverSlug($operatingSystem, $version = null)
107
    {
108
        $slug = static::$platforms[$operatingSystem]['slug'] ?? null;
109
110
        if (!is_null($version) && version_compare($version, '115.0', '<')) {
111
            if ($slug === 'mac-arm64') {
112
                return version_compare($version, '106.0.5249', '<') ? 'mac64_m1' : 'mac_arm64';
113
            } elseif ($slug === 'mac-x64') {
114
                return 'mac64';
115
            }
116
        }
117
118
        return $slug;
119
    }
120
121
    /**
122
     * Get all supported operating systems.
123
     *
124
     * @return array
125
     */
126
    public static function all()
127
    {
128
        return array_keys(static::$platforms);
129
    }
130
131
    /**
132
     * Get the current operating system identifier.
133
     *
134
     * @return string
135
     */
136
    public static function os()
137
    {
138
        if (static::onWindows()) {
139
            return 'win';
140
        } elseif (static::onMac()) {
141
            return static::macArchitectureId();
142
        }
143
144
        return 'linux';
145
    }
146
147
    /**
148
     * Determine if the operating system is Windows or Windows Subsystem for Linux.
149
     *
150
     * @return bool
151
     */
152
    public static function onWindows()
153
    {
154
        return PHP_OS === 'WINNT' || Str::contains(php_uname(), 'Microsoft');
155
    }
156
157
    /**
158
     * Determine if the operating system is macOS.
159
     *
160
     * @return bool
161
     */
162
    public static function onMac()
163
    {
164
        return PHP_OS === 'Darwin';
165
    }
166
167
    /**
168
     * Get the current macOS platform architecture.
169
     *
170
     * @return string
171
     */
172
    public static function macArchitectureId()
173
    {
174
        switch (php_uname('m')) {
175
            case 'arm64':
176
                return 'mac-arm';
177
            case 'x86_64':
178
                return 'mac-intel';
179
            default:
180
                return 'mac';
181
        }
182
    }
183
}
184