1 | <?php |
||
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); |
||
|
|||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $hostname |
||
89 | * @return $this |
||
90 | */ |
||
91 | 4 | public function hostname(string $hostname) |
|
96 | |||
97 | /** |
||
98 | * @param mixed $hostname |
||
99 | */ |
||
100 | 36 | private function setRealHostname(string $hostname) |
|
101 | { |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | 2 | public function getUser() |
|
112 | |||
113 | /** |
||
114 | * @param string $user |
||
115 | * @return $this |
||
116 | */ |
||
117 | 4 | public function user(string $user) |
|
122 | |||
123 | /** |
||
124 | * @return int |
||
125 | */ |
||
126 | 4 | public function getPort() |
|
130 | |||
131 | /** |
||
132 | * @param int $port |
||
133 | * @return $this |
||
134 | */ |
||
135 | 4 | public function port(int $port) |
|
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | 2 | public function getConfigFile() |
|
148 | |||
149 | /** |
||
150 | * @param string $configFile |
||
151 | * @return $this |
||
152 | */ |
||
153 | 3 | public function configFile(string $configFile) |
|
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | 3 | public function getIdentityFile() |
|
166 | |||
167 | /** |
||
168 | * @param string $identityFile |
||
169 | * @return $this |
||
170 | */ |
||
171 | 4 | public function identityFile(string $identityFile) |
|
176 | |||
177 | /** |
||
178 | * @return bool |
||
179 | */ |
||
180 | 2 | public function isForwardAgent() |
|
184 | |||
185 | /** |
||
186 | * @param bool $forwardAgent |
||
187 | * @return $this |
||
188 | */ |
||
189 | 3 | public function forwardAgent(bool $forwardAgent = true) |
|
194 | |||
195 | /** |
||
196 | * @return bool |
||
197 | */ |
||
198 | 2 | public function isMultiplexing() |
|
202 | |||
203 | /** |
||
204 | * @param bool $multiplexing |
||
205 | * @return $this |
||
206 | */ |
||
207 | 3 | public function multiplexing(bool $multiplexing = true) |
|
212 | |||
213 | 3 | public function getSshArguments() |
|
218 | |||
219 | 3 | public function sshOptions(array $options) : Host |
|
224 | |||
225 | 2 | public function sshFlags(array $flags) : Host |
|
230 | |||
231 | 1 | public function addSshOption(string $option, $value) : Host |
|
236 | |||
237 | public function addSshFlag(string $flag, string $value = null) : Host |
||
242 | |||
243 | public function getShellCommand() : string |
||
247 | |||
248 | /** |
||
249 | * @param string $shellCommand |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function shellCommand(string $shellCommand) |
||
257 | |||
258 | /** |
||
259 | * Set stage |
||
260 | * |
||
261 | * @param string $stage |
||
262 | * @return $this |
||
263 | */ |
||
264 | 5 | public function stage(string $stage) |
|
269 | |||
270 | /** |
||
271 | * Set roles |
||
272 | * |
||
273 | * @param array ...$roles |
||
274 | * @return $this |
||
275 | */ |
||
276 | 5 | public function roles(...$roles) |
|
286 | |||
287 | /** |
||
288 | * Set become |
||
289 | * |
||
290 | * @param string $user |
||
291 | * @return $this |
||
292 | */ |
||
293 | public function become(string $user) |
||
298 | } |
||
299 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.