Skip to content

Commit

Permalink
Implements SizeCalculator interface in FTP adapter
Browse files Browse the repository at this point in the history
See #417
  • Loading branch information
Albin Kerouanton authored and PedroTroller committed Mar 15, 2017
1 parent 8d277a1 commit 4cf76bb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions spec/Gaufrette/Adapter/FtpSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,16 @@ function it_fetches_keys_for_windows()

$this->keys()->shouldReturn(array('archive', 'file1.zip', 'file2.zip'));
}

function it_supports_sizecalculator()
{
$this->shouldImplement('Gaufrette\Adapter\SizeCalculator');

$this->size('/path')->shouldReturn(5000);
}

function it_throws_an_exception_when_size_cant_be_fetched()
{
$this->shouldThrow('RuntimeException')->during('size', ['/erroneous']);
}
}
9 changes: 9 additions & 0 deletions spec/Gaufrette/Adapter/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,12 @@ function apc_exists($path)

return true;
}

function ftp_size($connection, $path)
{
if ($path === '/erroneous') {
return -1;
}

return 5000;
}
21 changes: 20 additions & 1 deletion src/Gaufrette/Adapter/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*/
class Ftp implements Adapter,
FileFactory,
ListKeysAware
ListKeysAware,
SizeCalculator
{
protected $connection = null;
protected $directory;
Expand Down Expand Up @@ -292,6 +293,24 @@ public function createFile($key, Filesystem $filesystem)
return $file;
}

/**
* @param string $key
*
* @return int
*
* @throws \RuntimeException
*/
public function size($key)
{
$this->ensureDirectoryExists($this->directory, $this->create);

if (-1 === $size = ftp_size($this->connection, $key)) {
throw new \RuntimeException(sprintf('Unable to fetch the size of "%s".', $key));
}

return $size;
}

/**
* Ensures the specified directory exists. If it does not, and the create
* parameter is set to TRUE, it tries to create it.
Expand Down

0 comments on commit 4cf76bb

Please sign in to comment.