From f16ad454f8e0426ad6d8f18d4d04310922b9a78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin?= Date: Mon, 24 Apr 2023 15:37:10 +0200 Subject: [PATCH] Type GridFS and fix declaration checksum --- src/Gaufrette/Adapter/AzureBlobStorage.php | 2 +- src/Gaufrette/Adapter/ChecksumCalculator.php | 2 +- src/Gaufrette/Adapter/DoctrineDbal.php | 2 +- src/Gaufrette/Adapter/GridFS.php | 37 ++++++++------------ 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/Gaufrette/Adapter/AzureBlobStorage.php b/src/Gaufrette/Adapter/AzureBlobStorage.php index d9d81c32..fe4611a8 100644 --- a/src/Gaufrette/Adapter/AzureBlobStorage.php +++ b/src/Gaufrette/Adapter/AzureBlobStorage.php @@ -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); diff --git a/src/Gaufrette/Adapter/ChecksumCalculator.php b/src/Gaufrette/Adapter/ChecksumCalculator.php index cef02e12..177ac18d 100644 --- a/src/Gaufrette/Adapter/ChecksumCalculator.php +++ b/src/Gaufrette/Adapter/ChecksumCalculator.php @@ -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; } diff --git a/src/Gaufrette/Adapter/DoctrineDbal.php b/src/Gaufrette/Adapter/DoctrineDbal.php index dcb24a2d..c0495177 100644 --- a/src/Gaufrette/Adapter/DoctrineDbal.php +++ b/src/Gaufrette/Adapter/DoctrineDbal.php @@ -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'); } diff --git a/src/Gaufrette/Adapter/GridFS.php b/src/Gaufrette/Adapter/GridFS.php index de9bee6d..71b47fab 100644 --- a/src/Gaufrette/Adapter/GridFS.php +++ b/src/Gaufrette/Adapter/GridFS.php @@ -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); @@ -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)]); @@ -63,14 +58,12 @@ public function write($key, $content) } finally { fclose($stream); } - - return false; } /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { return false; } @@ -78,7 +71,7 @@ public function isDirectory($key) /** * {@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]); @@ -99,7 +92,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { return (boolean) $this->bucket->findOne(['filename' => $key]); } @@ -107,7 +100,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { $keys = []; $cursor = $this->bucket->find([], ['projection' => ['filename' => 1]]); @@ -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]]); @@ -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]]); @@ -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; @@ -156,7 +149,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function setMetadata($key, $metadata) + public function setMetadata(string $key, array $metadata): void { $this->metadata[$key] = $metadata; } @@ -164,7 +157,7 @@ public function setMetadata($key, $metadata) /** * {@inheritdoc} */ - public function getMetadata($key) + public function getMetadata(string $key): array { if (isset($this->metadata[$key])) { return $this->metadata[$key]; @@ -183,7 +176,7 @@ public function getMetadata($key) /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $prefix = trim($prefix); @@ -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;