1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TemplesOfCode\CodeSanity; |
4
|
|
|
|
5
|
|
|
use AFM\Rsync\SSH; |
6
|
|
|
use TemplesOfCode\Sofa\Command\ShellCommand; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RemoteConnection |
10
|
|
|
* @package TemplesOfCode\CodeSanity |
11
|
|
|
*/ |
12
|
|
|
class RemoteConnection extends SSH |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* RemoteConnection constructor. |
17
|
|
|
* @param array $options |
18
|
|
|
*/ |
19
|
24 |
|
public function __construct(array $options) |
20
|
|
|
{ |
21
|
24 |
|
parent::__construct($options); |
22
|
24 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param bool $hostConnection |
26
|
|
|
* @return ShellCommand |
27
|
|
|
*/ |
28
|
12 |
|
public function getCommand($hostConnection = true) |
29
|
|
|
{ |
30
|
12 |
|
if (is_null($this->username)) { |
31
|
1 |
|
throw new \InvalidArgumentException("You must specify a SSH username"); |
32
|
|
|
} |
33
|
|
|
|
34
|
11 |
|
if (is_null($this->host)) { |
35
|
1 |
|
throw new \InvalidArgumentException("You must specify a SSH host to connect"); |
36
|
|
|
} |
37
|
|
|
|
38
|
10 |
|
$command = new ShellCommand($this->executable); |
39
|
|
|
|
40
|
10 |
|
if ($this->port != 22) { |
41
|
1 |
|
$command->addArgument("p", $this->port); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
10 |
|
if (!is_null($this->publicKey)) { |
45
|
1 |
|
$command->addArgument("i", $this->publicKey); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
10 |
|
if ($hostConnection) { |
49
|
10 |
|
$command->addParameter($this->getHostConnection()); |
50
|
10 |
|
} |
51
|
|
|
|
52
|
10 |
|
return $command; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
2 |
|
public function isValid() |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* @var bool $success |
62
|
|
|
*/ |
63
|
2 |
|
$success = $this->testConnection(); |
64
|
2 |
|
if (!$success) { |
65
|
1 |
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var bool |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
2 |
|
protected function testConnection() |
76
|
|
|
{ |
77
|
|
|
/** |
78
|
|
|
* @var ShellCommand $sshCommand |
79
|
|
|
*/ |
80
|
2 |
|
$sshCommand = $this->buildSshCommand(); |
81
|
|
|
|
82
|
|
|
list( |
83
|
|
|
$exitStatus |
84
|
2 |
|
) = $sshCommand->execute(false); |
85
|
|
|
|
86
|
2 |
|
if ($exitStatus) { |
87
|
1 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return ShellCommand |
95
|
|
|
*/ |
96
|
3 |
|
protected function buildSshCommand() |
97
|
|
|
{ |
98
|
|
|
/** |
99
|
|
|
* @var ShellCommand $sshCommand |
100
|
|
|
*/ |
101
|
3 |
|
$sshCommand = $this->getCommand(true); |
102
|
3 |
|
$sshCommand->addOption('q'); |
103
|
3 |
|
$sshCommand->addParameter('exit'); |
104
|
|
|
|
105
|
3 |
|
return $sshCommand; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|