33namespace Jumper \Communicator ;
44
55use Jumper \Communicator ;
6+ use Jumper \Exception \CommunicatorException ;
67use Net_SSH2 as Ssh2Client ;
78
89/**
1415 */
1516class Ssh implements Communicator
1617{
18+ /**
19+ * @var \Net_SSH2
20+ */
1721 private $ ssh ;
1822
1923 /**
2024 * @var $authentication \Jumper\Communicator\Authentication
2125 */
2226 private $ authentication ;
2327
28+ /**
29+ * @var array
30+ */
2431 private $ defaultOptions = array (
2532 'host ' => '127.0.0.1 ' ,
2633 'port ' => '22 ' ,
@@ -29,6 +36,9 @@ class Ssh implements Communicator
2936 'callbacks ' => array ()
3037 );
3138
39+ /**
40+ * @param array $options
41+ */
3242 public function __construct (array $ options = array ())
3343 {
3444 $ this ->defaultOptions = array_replace_recursive ($ this ->defaultOptions , $ options );
@@ -39,34 +49,69 @@ public function __construct(array $options = array())
3949 );
4050 }
4151
52+ /**
53+ *
54+ */
4255 public function __destruct ()
4356 {
4457 $ this ->close ();
4558 }
4659
60+ /**
61+ * @param Authentication $authentication
62+ */
4763 public function setAuthentication (Authentication $ authentication )
4864 {
4965 $ this ->authentication = $ authentication ;
5066 }
5167
52- public function isConnected () {
68+ /**
69+ * @return bool
70+ */
71+ public function isConnected ()
72+ {
5373 return !is_null ($ this ->ssh ) && $ this ->ssh ->isConnected ();
5474 }
5575
76+ /**
77+ * @throws \Jumper\Exception\CommunicatorException
78+ */
5679 public function connect ()
5780 {
5881 $ authentication = null ;
5982 if (!is_null ($ this ->authentication )) {
6083 $ authentication = $ this ->authentication ->getAuthentication ($ this ->ssh );
6184 }
62- return $ this ->ssh ->login ($ this ->authentication ->getUser (), $ authentication );
85+ if (!$ this ->ssh ->login ($ this ->authentication ->getUser (), $ authentication )) {
86+ throw new CommunicatorException ($ this ->ssh ->getLastError (), $ this ->ssh ->getExitStatus ());
87+ }
6388 }
6489
90+ /**
91+ * @param $command
92+ *
93+ * @throws \RuntimeException
94+ * @throws \Jumper\Exception\CommunicatorException
95+ * @return String
96+ */
6597 public function run ($ command )
6698 {
67- return $ this ->ssh ->exec ($ command );
99+ $ result = $ this ->ssh ->exec ($ command );
100+ if ($ result === false ) {
101+ throw new CommunicatorException ($ this ->ssh ->getLastError (), $ this ->ssh ->getExitStatus ());
102+ }
103+
104+ $ error = $ this ->ssh ->getStdError ();
105+ if (!empty ($ error )) {
106+ throw new \RuntimeException ($ this ->ssh ->getStdError (), $ this ->ssh ->getExitStatus ());
107+ }
108+
109+ return $ result ;
68110 }
69111
112+ /**
113+ *
114+ */
70115 public function close ()
71116 {
72117 $ this ->ssh ->disconnect ();
0 commit comments