Skip to content

Commit

Permalink
Cleanup multiple code.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed May 20, 2014
1 parent 74919a2 commit 9ed50cd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 86 deletions.
112 changes: 44 additions & 68 deletions src/Support/Ftp.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
<?php namespace Orchestra\Support;

use Orchestra\Support\Ftp\Morph as Facade;
use Orchestra\Support\Ftp\ServerException;

class Ftp
{
/**
* FTP host.
*
* @var string
*/
protected $host = null;

/**
* FTP port.
*
* @var integer
*/
protected $port = 21;

/**
* FTP user.
*
* @var string
*/
protected $user = null;

/**
* FTP password.
*
* @var string
*/
protected $password = null;

/**
* FTP stream connection.
*
Expand All @@ -40,25 +13,19 @@ class Ftp
protected $connection = null;

/**
* FTP timeout.
*
* @var integer
*/
protected $timeout = 90;

/**
* FTP passive mode flag
* FTP configuration.
*
* @var boolean
* @var array
*/
protected $passive = false;

/**
* SSL-FTP connection flag.
*
* @var boolean
*/
protected $ssl = false;
protected $config = array(
'host' => null,
'port' => 21,
'user' => null,
'password' => null,
'timeout' => 90,
'passive' => false,
'ssl' => false,
);

/**
* System type of FTP server.
Expand Down Expand Up @@ -99,20 +66,16 @@ public function __construct($config = array())
*/
public function setUp($config = array())
{
$host = array_get($config, 'host');
$this->connection = array_pull($config, 'connection', $this->connection);

if (preg_match('/^(ftp|sftp):\/\/([a-zA-Z0-9\.\-_]*):?(\d{1,4})$/', $host, $matches)) {
if (preg_match('/^(ftp|sftp):\/\/([a-zA-Z0-9\.\-_]*):?(\d{1,4})$/', array_get($config, 'host'), $matches)) {
$config['host'] = $matches[2];
$config['ssl'] = ($matches[1] === 'sftp' ? true : false);

isset($matches[3]) && $config['port'] = $matches[3];
}

foreach ($config as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
$this->config = array_merge($this->config, $config);
}

/**
Expand Down Expand Up @@ -242,18 +205,25 @@ public function removeDirectory($directory)
*/
public function connect()
{
if (is_null($this->host)) {
$host = array_get($this->config, 'host');
$port = array_get($this->config, 'port');
$user = array_get($this->config, 'user');
$password = array_get($this->config, 'password');
$passive = array_get($this->config, 'passive');
$timeout = array_get($this->config, 'timeout');

if (is_null($host)) {
return ;
}

$this->createConnection();
$this->createConnection($host, $port, $timeout);

if (! (@Facade::login($this->connection, $this->user, $this->password))) {
throw new Ftp\ServerException("Failed FTP login to [{$this->host}].");
if (! (@Facade::login($this->connection, $user, $password))) {
throw new ServerException("Failed FTP login to [{$host}].");
}

// Set passive mode.
@Facade::pasv($this->connection, (bool) $this->passive);
@Facade::pasv($this->connection, (bool) $passive);

// Set system type.
$this->systemType = @Facade::systype($this->connection);
Expand All @@ -264,30 +234,36 @@ public function connect()
/**
* Create a FTP connection.
*
* @param string $host
* @param integer $port
* @param integer $timeout
* @return void
* @throws \Orchestra\Support\Ftp\Exception If unable to connect to FTP
* server.
* @throws \Orchestra\Support\Ftp\ServerException If unable to connect to FTP
* server.
*/
protected function createConnection()
protected function createConnection($host, $port = 21, $timeout = 90)
{
if ($this->ssl && @Facade::isCallable('sslConnect')) {
return $this->createSecureConnection();
} elseif (! ($this->connection = @Facade::connect($this->host, $this->port, $this->timeout))) {
throw new Ftp\ServerException("Failed to connect to [{$this->host}].");
if ($this->config['ssl'] && @Facade::isCallable('sslConnect')) {
return $this->createSecureConnection($host, $port, $timeout);
} elseif (! ($this->connection = @Facade::connect($host, $port, $timeout))) {
throw new ServerException("Failed to connect to [{$host}].");
}
}

/**
* Create a secure (SSL) FTP connection.
*
* @param string $host
* @param integer $port
* @param integer $timeout
* @return void
* @throws \Orchestra\Support\Ftp\ServerException
*/
protected function createSecureConnection()
protected function createSecureConnection($host, $port = 21, $timeout = 90)
{
if (!($this->connection = @Facade::sslConnect($this->host, $this->port, $this->timeout))) {
throw new Ftp\ServerException(
"Failed to connect to [{$this->host}] (SSL Connection)."
if (! ($this->connection = @Facade::sslConnect($host, $port, $timeout))) {
throw new ServerException(
"Failed to connect to [{$host}] (SSL Connection)."
);
}
}
Expand Down
30 changes: 12 additions & 18 deletions tests/FtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,18 @@ public function testInstanceOfFTP()
$stub->close();
$this->assertFalse($stub->connected());

$refl = new \ReflectionObject($this->stub);
$host = $refl->getProperty('host');
$port = $refl->getProperty('port');
$ssl = $refl->getProperty('ssl');
$user = $refl->getProperty('user');
$password = $refl->getProperty('password');

$host->setAccessible(true);
$port->setAccessible(true);
$ssl->setAccessible(true);
$user->setAccessible(true);
$password->setAccessible(true);

$this->assertEquals('localhost', $host->getValue($this->stub));
$this->assertTrue($ssl->getValue($this->stub));
$this->assertEquals('22', $port->getValue($this->stub));
$this->assertEquals('foo', $user->getValue($this->stub));
$this->assertEquals('foobar', $password->getValue($this->stub));
$refl = new \ReflectionObject($this->stub);
$config = $refl->getProperty('config');

$config->setAccessible(true);

$configuration = $config->getValue($this->stub);

$this->assertEquals('localhost', $configuration['host']);
$this->assertTrue($configuration['ssl']);
$this->assertEquals('22', $configuration['port']);
$this->assertEquals('foo', $configuration['user']);
$this->assertEquals('foobar', $configuration['password']);
}

/**
Expand Down

0 comments on commit 9ed50cd

Please sign in to comment.