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
Push — master ( 998592...a2c3f3 )
by
unknown
02:42
created

Client::generateControlPath()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 7
nop 1
dl 0
loc 32
ccs 0
cts 25
cp 0
crap 72
rs 5.3846
c 0
b 0
f 0
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) : Arguments
121
    {
122
        $sshArguments = $host->getSshArguments()->withMultiplexing($host);
123
        $controlPath  = $sshArguments->getOption('ControlPath');
124
125
        $process = new Process("ssh $sshArguments -O check -S $controlPath $host 2>&1");
126
        $process->run();
127
128
        if (!preg_match('/Master running/', $process->getOutput()) && $this->output->isVeryVerbose()) {
129
            $this->pop->writeln(Process::OUT, $host->getHostname(), 'ssh multiplexing initialization');
130
        }
131
132
        return $sshArguments;
133
    }
134
}
135