Skip to content

Commit

Permalink
Type GridFS and fix declaration checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Apr 24, 2023
1 parent 20b4484 commit f16ad45
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Gaufrette/Adapter/AzureBlobStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function mimeType(string $key): string
/**
* {@inheritdoc}
*/
public function checksum(string $key): string
public function checksum(string $key): string|bool
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand Down
2 changes: 1 addition & 1 deletion src/Gaufrette/Adapter/ChecksumCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface ChecksumCalculator
/**
* Returns the checksum of the specified key.
*/
public function checksum(string $key): string;
public function checksum(string $key): string|bool;
}
2 changes: 1 addition & 1 deletion src/Gaufrette/Adapter/DoctrineDbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function mtime(string $key): int|bool
/**
* {@inheritdoc}
*/
public function checksum(string $key): string
public function checksum(string $key): string|bool
{
return $this->getColumnValue($key, 'checksum');
}
Expand Down
37 changes: 15 additions & 22 deletions src/Gaufrette/Adapter/GridFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,22 @@
*/
class GridFS implements Adapter, ChecksumCalculator, MetadataSupporter, ListKeysAware, SizeCalculator
{
/** @var array */
private $metadata = [];

/** @var Bucket */
private $bucket;
private array $metadata = [];

/**
* @param Bucket $bucket
*/
public function __construct(Bucket $bucket)
public function __construct(private readonly Bucket $bucket)
{
if (!class_exists(Bucket::class)) {
throw new \LogicException('You need to install package "mongodb/mongodb" to use this adapter');
}
$this->bucket = $bucket;
}

/**
* {@inheritdoc}
*/
public function read($key)
public function read(string $key): string|bool
{
try {
$stream = $this->bucket->openDownloadStreamByName($key);
Expand All @@ -54,7 +49,7 @@ public function read($key)
/**
* {@inheritdoc}
*/
public function write($key, $content)
public function write(string $key, mixed $content): int|bool
{
$stream = $this->bucket->openUploadStream($key, ['metadata' => $this->getMetadata($key)]);

Expand All @@ -63,22 +58,20 @@ public function write($key, $content)
} finally {
fclose($stream);
}

return false;
}

/**
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
return false;
}

/**
* {@inheritdoc}
*/
public function rename($sourceKey, $targetKey)
public function rename(string $sourceKey, string $targetKey): bool
{
$metadata = $this->getMetadata($sourceKey);
$writable = $this->bucket->openUploadStream($targetKey, ['metadata' => $metadata]);
Expand All @@ -99,15 +92,15 @@ public function rename($sourceKey, $targetKey)
/**
* {@inheritdoc}
*/
public function exists($key)
public function exists(string $key): bool
{
return (boolean) $this->bucket->findOne(['filename' => $key]);
}

/**
* {@inheritdoc}
*/
public function keys()
public function keys(): array
{
$keys = [];
$cursor = $this->bucket->find([], ['projection' => ['filename' => 1]]);
Expand All @@ -122,7 +115,7 @@ public function keys()
/**
* {@inheritdoc}
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['uploadDate' => 1]]);

Expand All @@ -132,7 +125,7 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function checksum($key)
public function checksum(string $key): string|bool
{
$file = $this->bucket->findOne(['filename' => $key], ['projection' => ['md5' => 1]]);

Expand All @@ -142,7 +135,7 @@ public function checksum($key)
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
if (null === $file = $this->bucket->findOne(['filename' => $key], ['projection' => ['_id' => 1]])) {
return false;
Expand All @@ -156,15 +149,15 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function setMetadata($key, $metadata)
public function setMetadata(string $key, array $metadata): void
{
$this->metadata[$key] = $metadata;
}

/**
* {@inheritdoc}
*/
public function getMetadata($key)
public function getMetadata(string $key): array
{
if (isset($this->metadata[$key])) {
return $this->metadata[$key];
Expand All @@ -183,7 +176,7 @@ public function getMetadata($key)
/**
* {@inheritdoc}
*/
public function listKeys($prefix = '')
public function listKeys(string $prefix = ''): array
{
$prefix = trim($prefix);

Expand All @@ -208,7 +201,7 @@ public function listKeys($prefix = '')
return $result;
}

public function size($key)
public function size(string $key): int
{
if (!$this->exists($key)) {
return false;
Expand Down

0 comments on commit f16ad45

Please sign in to comment.