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.
Passed
Pull Request — master (#1219)
by Barry vd.
03:44
created

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 9.61%

Importance

Changes 0
Metric Value
dl 0
loc 127
ccs 5
cts 52
cp 0.0961
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseExitStatus() 0 12 2
A __construct() 0 6 1
B run() 0 59 6
A initMultiplexing() 0 22 3
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Ssh;
9
10
use Deployer\Exception\RuntimeException;
11
use Deployer\Host\Host;
12
use Deployer\Utility\ProcessOutputPrinter;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Process\Process;
15
16
class Client
17
{
18
    /**
19
     * @var OutputInterface
20
     */
21
    private $output;
22
23
    /**
24
     * @var ProcessOutputPrinter
25
     */
26
    private $pop;
27
28
    /**
29
     * @var bool
30
     */
31
    private $multiplexing;
32
33 6
    public function __construct(OutputInterface $output, ProcessOutputPrinter $pop, bool $multiplexing)
34
    {
35 6
        $this->output = $output;
36 6
        $this->pop = $pop;
37 6
        $this->multiplexing = $multiplexing;
38 6
    }
39
40
    /**
41
     * @param Host $host
42
     * @param string $command
43
     * @param array $config
44
     * @return string
45
     * @throws RuntimeException
46
     */
47
    public function run(Host $host, string $command, array $config = [])
48
    {
49
        $hostname = $host->getHostname();
50
        $defaults = [
51
            'timeout' => 300,
52
            'tty' => false,
53
        ];
54
        $config = array_merge($defaults, $config);
55
56
        $this->pop->command($hostname, $command);
57
58
        $sshArguments = $host->getSshArguments();
59
        $become = $host->has('become') ? 'sudo -u ' . $host->get('become') : '';
60
61
        // When tty need to be allocated, don't use multiplexing,
62
        // and pass command without bash allocation on remote host.
63
        if ($config['tty']) {
64
            $this->output->write(''); // Notify OutputWatcher
65
            $sshArguments = $sshArguments->withFlag('-tt');
66
            $command = escapeshellarg($command);
67
68
            $ssh = "ssh $sshArguments $host $command";
69
            $process = new Process($ssh);
70
            $process
71
                ->setTimeout($config['timeout'])
72
                ->setTty(true)
73
                ->mustRun();
74
75
            return $process->getOutput();
76
        }
77
78
        if ($host->isMultiplexing() === null ? $this->multiplexing : $host->isMultiplexing()) {
79
            $sshArguments = $this->initMultiplexing($host);
80
        }
81
82
        $ssh = "ssh $sshArguments $host $become 'bash -s; printf \"[exit_code:%s]\" $?;'";
83
84
        $process = new Process($ssh);
85
        $process
86
            ->setInput($command)
87
            ->setTimeout($config['timeout']);
88
89
        $process->run($this->pop->callback($hostname));
90
91
        $output = $this->pop->filterOutput($process->getOutput());
92
        $exitCode = $this->parseExitStatus($process);
93
94
        if ($exitCode !== 0) {
95
            throw new RuntimeException(
96
                $hostname,
97
                $command,
98
                $exitCode,
99
                $output,
100
                $process->getErrorOutput()
101
            );
102
        }
103
104
        return $output;
105
    }
106
107
    private function parseExitStatus(Process $process)
108
    {
109
        $output = $process->getOutput();
110
        preg_match('/\[exit_code:(.*?)\]/', $output, $match);
111
112
        if (!isset($match[1])) {
113
            return -1;
114
        }
115
116
        $exitCode = (int)$match[1];
117
        return $exitCode;
118
    }
119
120
    private function initMultiplexing(Host $host)
121
    {
122
        $sshArguments = $host->getSshArguments()->withMultiplexing($host);
123
124
        $process = new Process("ssh -O check $sshArguments $host 2>&1");
125
        $process->run();
126
127
        if (!preg_match('/Master running/', $process->getOutput())) {
128
            if ($this->output->isVeryVerbose()) {
129
                $this->pop->writeln(Process::OUT, $host->getHostname(), 'ssh multiplexing initialization');
130
            }
131
            
132
            // Open master connection explicit,
133
            // ControlMaster=auto could not working
134
            (new Process("ssh -M $sshArguments $host"))->start();
135
136
            // Delay to wait connection established
137
            sleep(1);
138
        }
139
140
        return $sshArguments;
141
    }
142
}
143