Skip to content

Commit

Permalink
Replace return values by exceptions in GridFS adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Albin Kerouanton committed Jan 5, 2018
1 parent 16971fc commit 2b8732e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
18 changes: 7 additions & 11 deletions spec/Gaufrette/Adapter/GridFSSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace spec\Gaufrette\Adapter;

use Gaufrette\Exception\StorageFailure;
use MongoDB\BSON\UTCDateTime;
use MongoDB\GridFS\Bucket;
use MongoDB\GridFS\Exception\FileNotFoundException;
use MongoDB\Model\BSONDocument;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class GridFSSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -53,18 +53,17 @@ function it_reads_file($bucket)

$bucket
->openDownloadStreamByName('filename')
->shouldBeCalled()
->willReturn($readable)
;

$this->read('filename')->shouldReturn('some content');
}

function it_does_not_fail_when_cannot_read($bucket)
function it_fails_when_cannot_read($bucket)
{
$bucket->openDownloadStreamByName('filename')->willThrow(FileNotFoundException::class);

$this->read('filename')->shouldReturn(false);
$this->shouldThrow(StorageFailure::class)->duringRead('filename');
}

function it_checks_if_file_exists($bucket, BSONDocument $file)
Expand Down Expand Up @@ -93,11 +92,11 @@ function it_deletes_file($bucket)
$this->delete('filename')->shouldReturn(true);
}

function it_does_not_delete_file($bucket)
function it_fails_when_file_to_delete_does_not_exist($bucket)
{
$bucket->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])->willReturn(null);

$this->delete('filename')->shouldReturn(false);
$this->shouldThrow(StorageFailure::class)->duringDelete('filename');
}

function it_writes_file($bucket)
Expand All @@ -110,10 +109,7 @@ function it_writes_file($bucket)
;

$this->setMetadata('filename', ['someother' => 'metadata']);
$this
->write('filename', 'some content')
->shouldReturn(12)
;
$this->write('filename', 'some content');
}

function it_renames_file($bucket)
Expand All @@ -133,7 +129,7 @@ function it_renames_file($bucket)
$bucket->delete(1234)->shouldBeCalled();

$this->setMetadata('filename', ['some' => 'metadata']);
$this->rename('filename', 'otherFilename')->shouldReturn(true);
$this->rename('filename', 'otherFilename');
}

function it_fetches_keys($bucket)
Expand Down
81 changes: 59 additions & 22 deletions src/Gaufrette/Adapter/GridFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Gaufrette\Adapter;

use Gaufrette\Adapter;
use Gaufrette\Exception\StorageFailure;
use MongoDB\BSON\Regex;
use MongoDB\GridFS\Bucket;
use MongoDB\GridFS\Exception\FileNotFoundException;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function read($key)
try {
$stream = $this->bucket->openDownloadStreamByName($key);
} catch (FileNotFoundException $e) {
return false;
throw StorageFailure::unexpectedFailure('read', ['key' => $key], $e);
}

try {
Expand All @@ -56,15 +57,14 @@ public function read($key)
*/
public function write($key, $content)
{
$stream = $this->bucket->openUploadStream($key, ['metadata' => $this->getMetadata($key)]);

try {
return fwrite($stream, $content);
} finally {
$stream = $this->bucket->openUploadStream($key, ['metadata' => $this->getMetadata($key)]);

fwrite($stream, $content);
fclose($stream);
} catch (\Exception $e) {
throw StorageFailure::unexpectedFailure('write', ['key' => $key], $e);
}

return false;
}

/**
Expand All @@ -81,27 +81,30 @@ public function isDirectory($key)
public function rename($sourceKey, $targetKey)
{
$metadata = $this->getMetadata($sourceKey);
$writable = $this->bucket->openUploadStream($targetKey, ['metadata' => $metadata]);

try {
$writable = $this->bucket->openUploadStream($targetKey, ['metadata' => $metadata]);
$this->bucket->downloadToStreamByName($sourceKey, $writable);

$this->setMetadata($targetKey, $metadata);
$this->delete($sourceKey);
} catch (FileNotFoundException $e) {
return false;
} finally {

fclose($writable);
} catch (FileNotFoundException $e) {
throw StorageFailure::unexpectedFailure('rename', ['sourceKey' => $sourceKey, 'targetKey' => $targetKey], $e);
}

return true;
}

/**
* {@inheritdoc}
*/
public function exists($key)
{
return (boolean) $this->bucket->findOne(['filename' => $key]);
try {
return $this->bucket->findOne(['filename' => $key]) !== null;
} catch (\Exception $e) {
throw StorageFailure::unexpectedFailure('exists', ['key' => $key], $e);
}
}

/**
Expand All @@ -110,7 +113,12 @@ public function exists($key)
public function keys()
{
$keys = [];
$cursor = $this->bucket->find([], ['projection' => ['filename' => 1]]);

try {
$cursor = $this->bucket->find([], ['projection' => ['filename' => 1]]);
} catch (\Exception $e) {
throw StorageFailure::unexpectedFailure('keys', [], $e);
}

foreach ($cursor as $file) {
$keys[] = $file['filename'];
Expand All @@ -124,28 +132,51 @@ public function keys()
*/
public function mtime($key)
{
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['uploadDate' => 1]]);
try {
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['uploadDate' => 1]]);
} catch (\Exception $e) {
throw StorageFailure::unexpectedFailure('mtime', ['key' => $key], $e);
}

if ($file === null) {
throw StorageFailure::unexpectedFailure('mtime', ['key' => $key]);
}

return $file ? (int) $file['uploadDate']->toDateTime()->format('U') : false;
return (int) $file['uploadDate']->toDateTime()->format('U');
}

/**
* {@inheritdoc}
*/
public function checksum($key)
{
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['md5' => 1]]);
try {
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['md5' => 1]]);
} catch (\Exception $e) {
throw StorageFailure::unexpectedFailure('checksum', ['key' => $key], $e);
}

if ($file === null) {
throw StorageFailure::unexpectedFailure('checksum', ['key' => $key]);
}

return $file ? $file['md5'] : false;
return $file['md5'];
}

/**
* {@inheritdoc}
*/
public function delete($key)
{
if (null === $file = $this->bucket->findOne(['filename' => $key], ['projection' => ['_id' => 1]])) {
return false;

try {
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['_id' => 1]]);
} catch (\Exception $e) {
throw StorageFailure::unexpectedFailure('delete', ['key' => $key], $e);
}

if ($file === null) {
throw StorageFailure::unexpectedFailure('delete', ['key' => $key]);
}

$this->bucket->delete($file['_id']);
Expand Down Expand Up @@ -193,7 +224,13 @@ public function listKeys($prefix = '')
}

$regex = new Regex(sprintf('^%s', $prefix), '');
$files = $this->bucket->find(['filename' => $regex], ['projection' => ['filename' => 1]]);

try {
$files = $this->bucket->find(['filename' => $regex], ['projection' => ['filename' => 1]]);
} catch (\Exception $e) {
throw StorageFailure::unexpectedFailure('listKeys', ['prefix' => $prefix]);
}

$result = [
'dirs' => [],
'keys' => [],
Expand Down

0 comments on commit 2b8732e

Please sign in to comment.