Passed
Pull Request — master (#19)
by Jonathan
14:43
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 Symfony\Component\Process\Process;
6
7
trait DetectsChromeVersion
8
{
9
    /**
10
     * The default commands to detect the installed Chrome/Chromium version.
11
     *
12
     * @var array
13
     */
14
    protected static $platforms = [
15
        'linux' => [
16
            'slug' => 'linux64',
17
            'commands' => [
18
                '/usr/bin/google-chrome --version',
19
                '/usr/bin/chromium-browser --version',
20
                '/usr/bin/chromium --version',
21
                '/usr/bin/google-chrome-stable --version',
22
            ],
23
        ],
24
        'mac' => [
25
            'slug' => 'mac-x64',
26
            'commands' => [
27
                '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version',
28
                '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
29
            ],
30
        ],
31
        'win' => [
32
            'slug' => 'win32',
33
            'commands' => [
34 3
                'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version',
35
            ],
36 3
        ],
37
    ];
38 3
39 2
    /**
40
     * Detect the installed Chrome/Chromium version.
41
     *
42
     * @param string $os
43
     * @return int|bool
44
     */
45 2
    protected function chromeVersion($os)
46
    {
47 1
        $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

47
        /** @scrutinizer ignore-call */ 
48
        $path = $this->option('detect');
Loading history...
48
49
        if ($path) {
50 3
            if ($os === 'win') {
51 3
                $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

51
                $this->/** @scrutinizer ignore-call */ 
52
                       error('Chrome version cannot be detected in custom installation path on Windows.');
Loading history...
52
53 3
                return false;
54
            }
55 3
56
            $commands = [$path.' --version'];
57 3
        } else {
58 1
            $commands = static::$platforms[$os]['commands'];
59
        }
60
61 2
        foreach ($commands as $command) {
62 2
            $process = Process::fromShellCommandline($command);
63
64
            $process->run();
65 2
66
            preg_match('/(\d+)(\.\d+){3}/', $process->getOutput(), $matches);
67
68 1
            if (!isset($matches[1])) {
69
                continue;
70 1
            }
71
72
            $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

72
            $this->/** @scrutinizer ignore-call */ 
73
                   comment(
Loading history...
73
                sprintf('Chrome version %s detected.', $matches[0])
74
            );
75
76
            return (int) $matches[1];
77
        }
78
79
        $this->error('Chrome version could not be detected. Please submit an issue: https://github.com/staudenmeir/dusk-updater');
80
81
        return false;
82
    }
83
84
    /**
85
     * Resolve the ChromeDriver slug for the given operating system.
86
     *
87
     * @param string $operatingSystem
88
     * @param string|null $version
89
     * @return string
90
     */
91
    public static function chromeDriverSlug($operatingSystem, $version = null)
92
    {
93
        $slug = static::$platforms[$operatingSystem]['slug'] ?? null;
94
95
        if (!is_null($version) && version_compare($version, '115.0', '<')) {
96
            if ($slug === 'mac-arm64') {
97
                return version_compare($version, '106.0.5249', '<') ? 'mac64_m1' : 'mac_arm64';
98
            } elseif ($slug === 'mac-x64') {
99
                return 'mac64';
100
            }
101
        }
102
103
        return $slug;
104
    }
105
106
    /**
107
     * Get all supported operating systems.
108
     *
109
     * @return array
110
     */
111
    public static function all()
112
    {
113
        return array_keys(static::$platforms);
114
    }
115
}
116