Skip to content

Commit

Permalink
Thrown a FileNotFoundException in rename and unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jan 10, 2024
1 parent 981ede2 commit 9f582b7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ In write mode, the stream context can contain the option ``gridfs['chunkSizeByte
If omitted, the defaults are inherited from the ``Bucket`` instance option.

The functions `rename` and `unlink` will rename or remove all revisions of a
filename. If the filename does not exist, these functions will return ``false``.
filename. If the filename does not exist, these functions throw a ``FileNotFoundException``.

Example
-------
Expand Down
11 changes: 11 additions & 0 deletions src/GridFS/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@

class FileNotFoundException extends RuntimeException
{
/**
* Thrown when a file cannot be found by its filename.
*
* @param string $filename Filename
* @return self
*/
public static function byStreamWrapperFilename(string $filename)
{
return new self(sprintf('File with name "%s" not found', $filename));
}

/**
* Thrown when a file cannot be found by its filename and revision.
*
Expand Down
23 changes: 19 additions & 4 deletions src/GridFS/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public static function register(string $protocol = 'gridfs'): void
/**
* Rename all revisions of a filename.
*
* @return bool True on success or false on failure.
* @return true
* @throws FileNotFoundException
*/
public function rename(string $fromPath, string $toPath): bool
{
Expand All @@ -110,9 +111,12 @@ public function rename(string $fromPath, string $toPath): bool
$newFilename = explode('/', $toPath, 4)[3] ?? '';
$count = $context['collectionWrapper']->updateFilenameForFilename($context['filename'], $newFilename);

// If $count === 0, the file does not exist.
if ($count === 0) {
throw FileNotFoundException::byStreamWrapperFilename($fromPath);
}

// If $count is null, the update is unacknowledged, the operation is considered successful.
return $count !== 0;
return true;
}

/**
Expand Down Expand Up @@ -293,12 +297,23 @@ public function stream_write(string $data): int
return $this->stream->writeBytes($data);
}

/**
* Remove all revisions of a filename.
*
* @return true
* @throws FileNotFoundException
*/
public function unlink(string $path): bool
{
$context = $this->getContext($path, 'w');
$count = $context['collectionWrapper']->deleteFileAndChunksByFilename($context['filename']);

return $count !== 0;
if ($count === 0) {
throw FileNotFoundException::byStreamWrapperFilename($path);
}

// If $count is null, the update is unacknowledged, the operation is considered successful.
return true;
}

/** @return false|array */
Expand Down
10 changes: 6 additions & 4 deletions tests/GridFS/StreamWrapperFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ public function testRenameAllRevisions(): void
$this->assertFalse(file_exists($path));
$this->assertSame('foobar', file_get_contents($path . '.renamed'));

$result = rename($path, $path . '.renamed');
$this->assertFalse($result, 'File does not exist anymore');
$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File with name "gridfs://bucket/filename" not found');
rename($path, $path . '.renamed');
}

public function testRenamePathMismatch(): void
Expand All @@ -406,7 +407,8 @@ public function testUnlinkAllRevisions(): void
$this->assertTrue($result);
$this->assertFalse(file_exists($path));

$result = unlink($path);
$this->assertFalse($result, 'File does not exist anymore');
$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File with name "gridfs://bucket/path/to/filename" not found');
unlink($path);
}
}

0 comments on commit 9f582b7

Please sign in to comment.