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

Arguments::getCliArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Deployer\Ssh;
4
5
use Deployer\Host\Host;
6
7
/**
8
 * @author Michael Woodward <[email protected]>
9
 */
10
class Arguments
11
{
12
    /**
13
     * @var array
14
     */
15
    private $flags = [];
16
17
    /**
18
     * @var array
19
     */
20
    private $options = [];
21
22 9
    public function getCliArguments() : string
23
    {
24 9
        $boolFlags  = array_keys(array_filter($this->flags, 'is_null'));
25
26 9
        $valueFlags = array_filter($this->flags);
27
        $valueFlags = array_map(function ($key, $value) {
28 4
            return "$key $value";
29 9
        }, array_keys($valueFlags), $valueFlags);
30
31
        $options    = array_map(function ($key, $value) {
32 7
            return "-o $key=$value";
33 9
        }, array_keys($this->options), $this->options);
34
35 9
        $args = sprintf('%s %s %s', implode(' ', $boolFlags), implode(' ', $valueFlags), implode(' ', $options));
36
37 9
        return trim(preg_replace('!\s+!', ' ', $args));
38
    }
39
40 3
    public function getOption(string $option) : string
41
    {
42 3
        return $this->options[$option] ?? '';
43
    }
44
45
    /**
46
     * @param string $flag
47
     * @return bool|mixed
48
     */
49 1
    public function getFlag(string $flag)
50
    {
51 1
        if (!array_key_exists($flag, $this->flags)) {
52
            return false;
53
        }
54
55 1
        return $this->flags[$flag] ?? true;
56
    }
57
58 8
    public function withFlags(array $flags) : Arguments
59
    {
60 8
        $clone = clone $this;
61 8
        $clone->flags = $this->buildFlagsFromArray($flags);
62
63 8
        return $clone;
64
    }
65
66 10
    public function withOptions(array $options) : Arguments
67
    {
68 10
        $clone = clone $this;
69 10
        $clone->options = $options;
70
71 10
        return $clone;
72
    }
73
74 4
    public function withFlag(string $flag, string $value = null) : Arguments
75
    {
76 4
        $clone = clone $this;
77 4
        $clone->flags = array_merge($this->flags, [$flag => $value]);
78
79 4
        return $clone;
80
    }
81
82 4
    public function withOption(string $option, string $value) : Arguments
83
    {
84 4
        $clone = clone $this;
85 4
        $clone->options = array_merge($this->options, [$option => $value]);
86
87 4
        return $clone;
88
    }
89
90 4
    public function withDefaults(Arguments $defaultOptions) : Arguments
91
    {
92 4
        $clone = clone $this;
93 4
        $clone->options = array_merge($defaultOptions->options, $this->options);
94 4
        $clone->flags = array_merge($defaultOptions->flags, $this->flags);
95
96 4
        return $clone;
97
    }
98
99 2
    public function withMultiplexing(Host $host) : Arguments
100
    {
101 2
        $controlPath = $this->generateControlPath($host);
102
103 2
        $multiplexDefaults = (new Arguments)->withOptions([
104 2
            'ControlMaster'  => 'auto',
105 2
            'ControlPersist' => '60',
106 2
            'ControlPath'    => $controlPath,
107
        ]);
108
109 2
        return $this->withDefaults($multiplexDefaults);
110
    }
111
112
    /**
113
     * Return SSH multiplexing control path
114
     *
115
     * When ControlPath is longer than 104 chars we can get:
116
     *
117
     *     SSH Error: unix_listener: too long for Unix domain socket
118
     *
119
     * So try to get as descriptive path as possible.
120
     * %C is for creating hash out of connection attributes.
121
     *
122
     * @param Host $host
123
     * @return string ControlPath
124
     * @throws Exception
125
     */
126 2
    private function generateControlPath(Host $host)
127
    {
128 2
        $connectionData = "$host{$host->getPort()}";
129 2
        $tryLongestPossible = 0;
130 2
        $controlPath = '';
131
        do {
132
            switch ($tryLongestPossible) {
133 2
                case 1:
134
                    $controlPath = "~/.ssh/deployer_mux_$connectionData";
135
                    break;
136 2
                case 2:
137
                    $controlPath = "~/.ssh/deployer_mux_%C";
138
                    break;
139 2
                case 3:
140
                    $controlPath = "~/deployer_mux_$connectionData";
141
                    break;
142 2
                case 4:
143
                    $controlPath = "~/deployer_mux_%C";
144
                    break;
145 2
                case 5:
146
                    $controlPath = "~/mux_%C";
147
                    break;
148 2
                case 6:
149
                    throw new Exception("The multiplexing control path is too long. Control path is: $controlPath");
150
                default:
151 2
                    $controlPath = "~/.ssh/deployer_mux_$connectionData";
152
            }
153 2
            $tryLongestPossible++;
154 2
        } while (strlen($controlPath) > 104); // Unix socket max length
155
156 2
        return $controlPath;
157
    }
158
159 8
    private function buildFlagsFromArray($flags) : array
160
    {
161
        $boolFlags = array_filter(array_map(function ($key, $value) {
162 8
            if (is_int($key)) {
163 6
                return $value;
164
            }
165
166 4
            if (null === $value) {
167 3
                return $key;
168
            }
169 8
        }, array_keys($flags), $flags));
170
171 8
        $valueFlags = array_filter($flags, function ($key, $value) {
172 8
            return is_string($key) && is_string($value);
173 8
        }, ARRAY_FILTER_USE_BOTH);
174
175 8
        return array_merge(array_fill_keys($boolFlags, null), $valueFlags);
176
    }
177
178
    public function __toString() : string
179
    {
180
        return $this->getCliArguments();
181
    }
182
}
183