remove code duplication for listShares

This commit is contained in:
Robin Appelman 2016-08-26 23:45:29 +02:00
commit b43c129ed9
4 changed files with 58 additions and 55 deletions

View file

@ -17,6 +17,14 @@ class Connection extends RawConnection {
const DELIMITER = 'smb:';
const DELIMITER_LENGTH = 4;
/** @var Parser */
private $parser;
public function __construct($command, Parser $parser, $env = array()) {
parent::__construct($command, $env);
$this->parser = $parser;
}
/**
* send input to smbclient
*
@ -42,7 +50,7 @@ class Connection extends RawConnection {
throw new ConnectionException('Connection not valid');
}
$promptLine = $this->readLine(); //first line is prompt
$this->checkConnectionError($promptLine);
$this->parser->checkConnectionError($promptLine);
$output = array();
$line = $this->readLine();
@ -90,33 +98,6 @@ class Connection extends RawConnection {
}
}
/**
* check if the first line holds a connection failure
*
* @param $line
* @throws AuthenticationException
* @throws InvalidHostException
* @throws NoLoginServerException
*/
private function checkConnectionError($line) {
$line = rtrim($line, ')');
if (substr($line, -23) === ErrorCodes::LogonFailure) {
throw new AuthenticationException('Invalid login');
}
if (substr($line, -26) === ErrorCodes::BadHostName) {
throw new InvalidHostException('Invalid hostname');
}
if (substr($line, -22) === ErrorCodes::Unsuccessful) {
throw new InvalidHostException('Connection unsuccessful');
}
if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
throw new InvalidHostException('Connection refused');
}
if (substr($line, -26) === ErrorCodes::NoLogonServers) {
throw new NoLoginServerException('No login server');
}
}
public function close($terminate = true) {
if (is_resource($this->getInputStream())) {
$this->write('close' . PHP_EOL);