GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

StartServerService::downloadDrivers()   C
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 1 Features 4
Metric Value
c 10
b 1
f 4
dl 0
loc 47
rs 5.2941
cc 9
eloc 27
nc 6
nop 0
1
<?php
2
namespace SeleniumSetup\Service;
3
4
use SeleniumSetup\Locker\ServerItemFactory;
5
6
class StartServerService extends AbstractService
7
{
8
    protected function createFolders()
9
    {
10
        // Create the build folder. (Where the binaries will reside).
11
        if (!$this->fileSystem->isDir(($this->config->getBuildPath()))) {
12
            $this->fileSystem->createDir($this->config->getBuildPath());
13
        }
14
        
15
        // Create the tmp folder.
16
        if (!$this->fileSystem->isDir(($this->config->getTmpPath()))) {
17
            $this->fileSystem->createDir($this->config->getTmpPath());
18
        }
19
        
20
        // Create the logs folder.
21
        if (!$this->fileSystem->isDir($this->config->getLogsPath())) {
22
            $this->fileSystem->createDir($this->config->getLogsPath());
23
        }
24
    }
25
26
    protected function downloadDrivers()
27
    {
28
        foreach ($this->config->getBinaries() as $binary) {
29
            // Skip binaries that don't belong to the current operating system.
30
            if (
31
                !is_null($binary->getOs()) && $binary->getOs() != $this->env->getOsName() ||
32
                !is_null($binary->getOsType()) && $binary->getOsType() != $this->env->getOsType()
33
            ) {
34
                continue;
35
            }
36
37
            $binaryPath = $this->config->getBuildPath() . DIRECTORY_SEPARATOR . $binary->getBinName();
38
39
            if (!$this->fileSystem->isFile($binaryPath)) {
40
                $this->output->writeln(
41
                    sprintf('Downloading %s %s ...', $binary->getLabel(), $binary->getVersion())
42
                );
43
                
44
                // Download.
45
                $downloadTo = $this->config->getBuildPath() . DIRECTORY_SEPARATOR . pathinfo($binary->getDownloadUrl(), PATHINFO_BASENAME);
46
                $download = $this->env->download($binary->getDownloadUrl(), $downloadTo);
47
                $this->output->writeln($download);
48
49
                // Unzip.
50
                if (in_array(pathinfo($binary->getDownloadUrl(), PATHINFO_EXTENSION), ['zip', 'tar', 'tar.gz'])) {
51
                    $zip = new \ZipArchive;
52
                    $res = $zip->open($downloadTo);
53
                    if ($res === true) {
54
                        $zip->extractTo(
55
                            $this->config->getBuildPath(),
56
                            [$binary->getBinName()]
57
                        );
58
                        $zip->close();
59
                    }
60
                } else {
61
                    $this->fileSystem->rename($downloadTo, $this->config->getBuildPath() . DIRECTORY_SEPARATOR . $binary->getBinName());
62
                }
63
64
                // Make executable.
65
                $this->env->makeExecutable($this->config->getBuildPath() . DIRECTORY_SEPARATOR . $binary->getBinName());
66
            } else {
67
                $this->output->writeln(
68
                    sprintf('Skipping %s %s. Binary already exists.', $binary->getLabel(), $binary->getVersion())
69
                );
70
            }
71
        }
72
    }
73
    
74
    public function test()
75
    {
76
        return $this->env->test();
77
    }
78
    
79
    public function handle()
80
    {
81
        // @todo should allow only registered instances to be called
82
83
        $this->createFolders();
84
        $this->downloadDrivers();
85
86
        // Add build folder to path.
87
        $this->env->addPathToGlobalPath($this->config->getBuildPath());
88
        
89
        // Warn if Chrome or Firefox binaries are not available.
90
        if ($chromeVersion = $this->env->getChromeVersion()) {
91
            $this->output->writeln('Chrome binary found, v.' . $chromeVersion);
92
        } else {
93
            $this->output->writeln('<info>WARNING: Chrome binary not found.</info>');
94
        }
95
96
        if ($firefoxVersion = $this->env->getFirefoxVersion()) {
97
            $this->output->writeln('Firefox binary found, v.' . $firefoxVersion);
98
        } else {
99
            $this->output->writeln('<info>WARNING: Firefox binary not found.</info>');
100
        }
101
        
102
        // Warn if display is not available.
103
        if ($this->env->isLinux()) {
104
            if ($this->env->hasXvfb()) {
105
                $this->output->writeln('<info>Xvfb is installed. Good.</info>');
106
            } else {
107
                $this->output->writeln('<info>WARNING: Xvfb is not installed.</info>');
108
            }
109
        }
110
        
111
        // $pid = $this->env->startDisplayProcess();
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
113
        // Start Selenium Server instance.
114
        $this->output->writeln(
115
            sprintf('Starting Selenium Server (%s) ... %s:%s', $this->config->getName(), $this->config->getHostname(), $this->config->getPort())
116
        );
117
118
        $pid = $this->env->startSeleniumProcess();
119
        if ($pid > 0) {
120
            // Make sure that we capture the right PID.
121
            while ($this->env->listenToPort($this->config->getPort()) == '') {
122
                $this->output->write('<info>.</info>');
123
            }
124
            $parentPid = $this->env->getPidFromListeningToPort($this->config->getPort());
125
            if ($parentPid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parentPid of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
126
                $pid = $parentPid;
127
            }
128
129
            // @todo open the lock file only to update the status and ports, the instance was already added.
130
            $this->locker->openLockFile();
131
            $this->locker->addServer(
132
                ServerItemFactory::createFromProperties(
133
                    $this->config->getName(),
134
                    $pid,
135
                    $this->config->getPort(),
136
                    $this->config->getFilePath()
137
                )
138
            );
139
            $this->locker->writeToLockFile();
140
        }
141
142
        $this->output->writeln('<info>Done</info>');
143
        $this->output->writeln(
144
            sprintf('Test it at http://%s:%s/wd/hub/', $this->config->getHostname(), $this->config->getPort())
145
        );
146
    }
147
}