Skip to content

Commit

Permalink
Replace return values by exceptions in Zip adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Albin Kerouanton committed Jan 11, 2018
1 parent 1092103 commit c44ea77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Gaufrette/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function keys();
*
* @return int An UNIX like timestamp
*
* @throws FileNotFound
* @throws InvalidKey If $key is malforme
* @throws FileNotFound If the file does not exist
* @throws InvalidKey If $key is invalid or malformed
* @throws StorageFailure If the underlying storage fails (adapter should not leak exceptions)
*/
public function mtime($key);
Expand All @@ -77,7 +77,7 @@ public function mtime($key);
*
* @param string $key
*
* @throws FileNotFound
* @throws FileNotFound If the file does not exist.
* @throws InvalidKey If $key is malformed
* @throws StorageFailure If the underlying storage fails (adapter should not leak exceptions)
*/
Expand Down
31 changes: 21 additions & 10 deletions src/Gaufrette/Adapter/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Gaufrette\Adapter;

use Gaufrette\Exception\FileNotFound;
use Gaufrette\Exception\StorageFailure;
use ZipArchive;
use Gaufrette\Adapter;
use Gaufrette\Util;
Expand Down Expand Up @@ -43,7 +45,7 @@ public function __construct($zipFile)
public function read($key)
{
if (false === ($content = $this->zipArchive->getFromName($key, 0))) {
return false;
throw new FileNotFound($key);
}

return $content;
Expand All @@ -55,14 +57,12 @@ public function read($key)
public function write($key, $content)
{
if (!$this->zipArchive->addFromString($key, $content)) {
return false;
throw StorageFailure::unexpectedFailure('write', ['key' => $key]);
}

if (!$this->save()) {
return false;
throw StorageFailure::unexpectedFailure('write', ['key' => $key]);
}

return Util\Size::fromContent($content);
}

/**
Expand Down Expand Up @@ -104,7 +104,11 @@ public function mtime($key)
{
$stat = $this->getStat($key);

return isset($stat['mtime']) ? $stat['mtime'] : false;
if (!isset($stat['mtime'])) {
throw new FileNotFound($key);
}

return $stat['mtime'];
}

/**
Expand All @@ -113,10 +117,12 @@ public function mtime($key)
public function delete($key)
{
if (!$this->zipArchive->deleteName($key)) {
return false;
throw new FileNotFound($key);
}

return $this->save();
if ($this->save() === false) {
throw StorageFailure::unexpectedFailure('delete', ['key' => $key]);
}
}

/**
Expand All @@ -125,10 +131,15 @@ public function delete($key)
public function rename($sourceKey, $targetKey)
{
if (!$this->zipArchive->renameName($sourceKey, $targetKey)) {
return false;
throw new FileNotFound($key);
}

return $this->save();
if ($this->save() === false) {
throw StorageFailure::unexpectedFailure('rename', [
'sourceKey' => $sourceKey,
'targetKey' => $targetKey,
]);
}
}

/**
Expand Down

0 comments on commit c44ea77

Please # to comment.