public function connect()
{
$serverConfig = $this->getConfiguration();
$this->sftp = new SFTP($serverConfig->getHost(), $serverConfig->getPort(), 3600);
switch ($serverConfig->getAuthenticationMethod()) {
case Configuration::AUTH_BY_PASSWORD:
$result = $this->sftp->login($serverConfig->getUser(), $serverConfig->getPassword());
break;
case Configuration::AUTH_BY_IDENTITY_FILE:
$key = new RSA();
$key->setPassword($serverConfig->getPassPhrase());
$key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
$result = $this->sftp->login($serverConfig->getUser(), $key);
break;
case Configuration::AUTH_BY_PEM_FILE:
$key = new RSA();
$key->loadKey(file_get_contents($serverConfig->getPemFile()));
$result = $this->sftp->login($serverConfig->getUser(), $key);
break;
case Configuration::AUTH_BY_AGENT:
$key = new Agent();
$key->startSSHForwarding(null);
$result = $this->sftp->login($serverConfig->getUser(), $key);
break;
case Configuration::AUTH_BY_IDENTITY_FILE_AND_PASSWORD:
$key = new RSA();
$key->setPassword($serverConfig->getPassPhrase());
$key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
$result = $this->sftp->login($serverConfig->getUser(), $key, $serverConfig->getPassword());
break;
default:
throw new RuntimeException('You need to specify authentication method.');
}
if (!$result) {
throw new RuntimeException('Unable to login with the provided credentials.');
}
}