Passed
Push — master ( 88e408...4d0df3 )
by Anton
04:53 queued 02:21
created

src/Host/Host.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Host;
9
10
use Deployer\Configuration\Configuration;
11
use Deployer\Configuration\ConfigurationAccessor;
12
use Deployer\Ssh\Arguments;
13
use function Deployer\Support\array_flatten;
14
15
class Host
16
{
17
    use ConfigurationAccessor;
18
19
    private $hostname;
20
    private $realHostname;
21
    private $user;
22
    private $port;
23
    private $configFile;
24
    private $identityFile;
25
    private $forwardAgent = true;
26
    private $multiplexing = null;
27
    private $sshArguments;
28
    private $shellCommand = 'bash -s';
29
30
    /**
31
     * @param string $hostname
32
     */
33 36
    public function __construct(string $hostname)
34
    {
35 36
        $this->hostname = $hostname;
36 36
        $this->setRealHostname($hostname);
37 36
        $this->config = new Configuration();
38 36
        $this->sshArguments = new Arguments();
39 36
    }
40
41 3
    private function initOptions()
42
    {
43 3
        if ($this->port) {
44 3
            $this->sshArguments = $this->sshArguments->withFlag('-p', $this->port);
45
        }
46
47 3
        if ($this->configFile) {
48 2
            $this->sshArguments = $this->sshArguments->withFlag('-F', $this->configFile);
49
        }
50
51 3
        if ($this->identityFile) {
52 2
            $this->sshArguments = $this->sshArguments->withFlag('-i', $this->getIdentityFile());
53
        }
54
55 3
        if ($this->forwardAgent) {
56 3
            $this->sshArguments = $this->sshArguments->withFlag('-A');
57
        }
58 3
    }
59
60
    /**
61
     * Returns pair user/hostname
62
     *
63
     * @return string
64
     */
65 6
    public function __toString()
66
    {
67 6
        $user = empty($this->user) ? '' : "{$this->user}@";
68 6
        return "$user{$this->realHostname}";
69
    }
70
71
    /**
72
     * @return string
73
     */
74 30
    public function getHostname()
75
    {
76 30
        return $this->config->parse($this->hostname);
77
    }
78
79
    /**
80
     * @return mixed
81
     */
82
    public function getRealHostname()
83
    {
84
        return $this->config->parse($this->realHostname);
0 ignored issues
show
It seems like $this->realHostname can also be of type array<integer,string>; however, Deployer\Configuration\Configuration::parse() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
    }
86
87
    /**
88
     * @param string $hostname
89
     * @return $this
90
     */
91 4
    public function hostname(string $hostname)
92
    {
93 4
        $this->setRealHostname($hostname);
94 4
        return $this;
95
    }
96
97
    /**
98
     * @param mixed $hostname
99
     */
100 36
    private function setRealHostname(string $hostname)
101
    {
102 36
        $this->realHostname = preg_replace('/\/.+$/', '', $hostname);
103 36
    }
104
105
    /**
106
     * @return string
107
     */
108 2
    public function getUser()
109
    {
110 2
        return $this->config->parse($this->user);
111
    }
112
113
    /**
114
     * @param string $user
115
     * @return $this
116
     */
117 4
    public function user(string $user)
118
    {
119 4
        $this->user = $user;
120 4
        return $this;
121
    }
122
123
    /**
124
     * @return int
125
     */
126 4
    public function getPort()
127
    {
128 4
        return $this->port;
129
    }
130
131
    /**
132
     * @param int $port
133
     * @return $this
134
     */
135 4
    public function port(int $port)
136
    {
137 4
        $this->port = $port;
138 4
        return $this;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 2
    public function getConfigFile()
145
    {
146 2
        return $this->configFile;
147
    }
148
149
    /**
150
     * @param string $configFile
151
     * @return $this
152
     */
153 3
    public function configFile(string $configFile)
154
    {
155 3
        $this->configFile = $configFile;
156 3
        return $this;
157
    }
158
159
    /**
160
     * @return string
161
     */
162 3
    public function getIdentityFile()
163
    {
164 3
        return $this->config->parse($this->identityFile);
165
    }
166
167
    /**
168
     * @param string $identityFile
169
     * @return $this
170
     */
171 4
    public function identityFile(string $identityFile)
172
    {
173 4
        $this->identityFile = $identityFile;
174 4
        return $this;
175
    }
176
177
    /**
178
     * @return bool
179
     */
180 2
    public function isForwardAgent()
181
    {
182 2
        return $this->forwardAgent;
183
    }
184
185
    /**
186
     * @param bool $forwardAgent
187
     * @return $this
188
     */
189 3
    public function forwardAgent(bool $forwardAgent = true)
190
    {
191 3
        $this->forwardAgent = $forwardAgent;
192 3
        return $this;
193
    }
194
195
    /**
196
     * @return bool
197
     */
198 2
    public function isMultiplexing()
199
    {
200 2
        return $this->multiplexing;
201
    }
202
203
    /**
204
     * @param bool $multiplexing
205
     * @return $this
206
     */
207 3
    public function multiplexing(bool $multiplexing = true)
208
    {
209 3
        $this->multiplexing = $multiplexing;
210 3
        return $this;
211
    }
212
213 3
    public function getSshArguments()
214
    {
215 3
        $this->initOptions();
216 3
        return $this->sshArguments;
217
    }
218
219 3
    public function sshOptions(array $options) : Host
220
    {
221 3
        $this->sshArguments = $this->sshArguments->withOptions($options);
222 3
        return $this;
223
    }
224
225 2
    public function sshFlags(array $flags) : Host
226
    {
227 2
        $this->sshArguments = $this->sshArguments->withFlags($flags);
228 2
        return $this;
229
    }
230
231 1
    public function addSshOption(string $option, $value) : Host
232
    {
233 1
        $this->sshArguments = $this->sshArguments->withOption($option, $value);
234 1
        return $this;
235
    }
236
237
    public function addSshFlag(string $flag, string $value = null) : Host
238
    {
239
        $this->sshArguments = $this->sshArguments->withFlag($flag, $value);
240
        return $this;
241
    }
242
243
    public function getShellCommand() : string
244
    {
245
        return $this->shellCommand;
246
    }
247
248
    /**
249
     * @param string $shellCommand
250
     * @return $this
251
     */
252
    public function shellCommand(string $shellCommand)
253
    {
254
        $this->shellCommand = $shellCommand;
255
        return $this;
256
    }
257
258
    /**
259
     * Set stage
260
     *
261
     * @param string $stage
262
     * @return $this
263
     */
264 5
    public function stage(string $stage)
265
    {
266 5
        $this->config->set('stage', $stage);
267 5
        return $this;
268
    }
269
270
    /**
271
     * Set roles
272
     *
273
     * @param array ...$roles
274
     * @return $this
275
     */
276 5
    public function roles(...$roles)
277
    {
278 5
        $this->config->set('roles', []);
279
280 5
        foreach (array_flatten($roles) as $role) {
281 5
            $this->config->add('roles', [$role]);
282
        }
283
284 5
        return $this;
285
    }
286
287
    /**
288
     * Set become
289
     *
290
     * @param string $user
291
     * @return $this
292
     */
293
    public function become(string $user)
294
    {
295
        $this->config->set('become', $user);
296
        return $this;
297
    }
298
}
299