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 | 37 | public function __construct(string $hostname) |
|
34 | { |
||
35 | 37 | $this->hostname = $hostname; |
|
36 | 37 | $this->setRealHostname($hostname); |
|
37 | 37 | $this->config = new Configuration(); |
|
38 | 37 | $this->sshArguments = new Arguments(); |
|
39 | 37 | } |
|
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 | 31 | public function getHostname() |
|
75 | { |
||
76 | 31 | 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
|
|||
85 | } |
||
86 | |||
87 | 4 | public function hostname(string $hostname): self |
|
88 | { |
||
89 | 4 | $this->setRealHostname($hostname); |
|
90 | 4 | return $this; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param mixed $hostname |
||
95 | */ |
||
96 | 37 | private function setRealHostname(string $hostname) |
|
97 | { |
||
98 | 37 | $this->realHostname = preg_replace('/\/.+$/', '', $hostname); |
|
99 | 37 | } |
|
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | 2 | public function getUser() |
|
105 | { |
||
106 | 2 | return $this->config->parse($this->user); |
|
107 | } |
||
108 | |||
109 | 4 | public function user(string $user): self |
|
110 | { |
||
111 | 4 | $this->user = $user; |
|
112 | 4 | return $this; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return int |
||
117 | */ |
||
118 | 4 | public function getPort() |
|
119 | { |
||
120 | 4 | return $this->port; |
|
121 | } |
||
122 | |||
123 | 4 | public function port(int $port): self |
|
124 | { |
||
125 | 4 | $this->port = $port; |
|
126 | 4 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | 2 | public function getConfigFile() |
|
133 | { |
||
134 | 2 | return $this->configFile; |
|
135 | } |
||
136 | |||
137 | 3 | public function configFile(string $configFile): self |
|
138 | { |
||
139 | 3 | $this->configFile = $configFile; |
|
140 | 3 | return $this; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | 3 | public function getIdentityFile() |
|
147 | { |
||
148 | 3 | return $this->config->parse($this->identityFile); |
|
149 | } |
||
150 | |||
151 | 4 | public function identityFile(string $identityFile): self |
|
152 | { |
||
153 | 4 | $this->identityFile = $identityFile; |
|
154 | 4 | return $this; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return bool |
||
159 | */ |
||
160 | 2 | public function isForwardAgent() |
|
161 | { |
||
162 | 2 | return $this->forwardAgent; |
|
163 | } |
||
164 | |||
165 | 3 | public function forwardAgent(bool $forwardAgent = true): self |
|
166 | { |
||
167 | 3 | $this->forwardAgent = $forwardAgent; |
|
168 | 3 | return $this; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return bool |
||
173 | */ |
||
174 | 2 | public function isMultiplexing() |
|
175 | { |
||
176 | 2 | return $this->multiplexing; |
|
177 | } |
||
178 | |||
179 | 3 | public function multiplexing(bool $multiplexing = true): self |
|
180 | { |
||
181 | 3 | $this->multiplexing = $multiplexing; |
|
182 | 3 | return $this; |
|
183 | } |
||
184 | |||
185 | 3 | public function getSshArguments() |
|
186 | { |
||
187 | 3 | $this->initOptions(); |
|
188 | 3 | return $this->sshArguments; |
|
189 | } |
||
190 | |||
191 | 3 | public function sshOptions(array $options): self |
|
192 | { |
||
193 | 3 | $this->sshArguments = $this->sshArguments->withOptions($options); |
|
194 | 3 | return $this; |
|
195 | } |
||
196 | |||
197 | 2 | public function sshFlags(array $flags): self |
|
198 | { |
||
199 | 2 | $this->sshArguments = $this->sshArguments->withFlags($flags); |
|
200 | 2 | return $this; |
|
201 | } |
||
202 | |||
203 | 1 | public function addSshOption(string $option, $value): self |
|
204 | { |
||
205 | 1 | $this->sshArguments = $this->sshArguments->withOption($option, $value); |
|
206 | 1 | return $this; |
|
207 | } |
||
208 | |||
209 | public function addSshFlag(string $flag, string $value = null): self |
||
210 | { |
||
211 | $this->sshArguments = $this->sshArguments->withFlag($flag, $value); |
||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | public function getShellCommand() : string |
||
216 | { |
||
217 | return $this->shellCommand; |
||
218 | } |
||
219 | |||
220 | public function shellCommand(string $shellCommand): self |
||
221 | { |
||
222 | $this->shellCommand = $shellCommand; |
||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | 5 | public function stage(string $stage): self |
|
227 | { |
||
228 | 5 | $this->config->set('stage', $stage); |
|
229 | 5 | return $this; |
|
230 | } |
||
231 | |||
232 | 5 | public function roles(...$roles): self |
|
233 | { |
||
234 | 5 | $this->config->set('roles', []); |
|
235 | |||
236 | 5 | foreach (array_flatten($roles) as $role) { |
|
237 | 5 | $this->config->add('roles', [$role]); |
|
238 | } |
||
239 | |||
240 | 5 | return $this; |
|
241 | } |
||
242 | |||
243 | public function become(string $user): self |
||
244 | { |
||
245 | $this->config->set('become', $user); |
||
246 | return $this; |
||
247 | } |
||
248 | } |
||
249 |
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.