diff --git a/src/Gaufrette/Adapter/AsyncAwsS3.php b/src/Gaufrette/Adapter/AsyncAwsS3.php index c0979a2f..b8cec132 100644 --- a/src/Gaufrette/Adapter/AsyncAwsS3.php +++ b/src/Gaufrette/Adapter/AsyncAwsS3.php @@ -43,7 +43,7 @@ public function __construct( /** * {@inheritdoc} */ - public function setMetadata(string $key, array $content) + public function setMetadata(string $key, array $content): void { // BC with AmazonS3 adapter if (isset($content['contentType'])) { diff --git a/src/Gaufrette/Adapter/AwsS3.php b/src/Gaufrette/Adapter/AwsS3.php index 2d335a1c..8a58a4fb 100644 --- a/src/Gaufrette/Adapter/AwsS3.php +++ b/src/Gaufrette/Adapter/AwsS3.php @@ -13,32 +13,21 @@ */ class AwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider { - /** @var S3Client */ - protected $service; - /** @var string */ - protected $bucket; - /** @var array */ - protected $options; - /** @var bool */ - protected $bucketExists; - /** @var array */ - protected $metadata = []; - /** @var bool */ - protected $detectContentType; - - /** - * @param S3Client $service - * @param string $bucket - * @param array $options - * @param bool $detectContentType - */ - public function __construct(S3Client $service, $bucket, array $options = [], $detectContentType = false) - { + protected S3Client $service; + protected array $options; + protected bool $bucketExists; + protected array $metadata = []; + + public function __construct( + S3Client $service, + private readonly string $bucket, + array $options = [], + private readonly bool $detectContentType = false + ) { if (!class_exists(S3Client::class)) { throw new \LogicException('You need to install package "aws/aws-sdk-php" to use this adapter'); } $this->service = $service; - $this->bucket = $bucket; $this->options = array_replace( [ 'create' => false, @@ -47,14 +36,12 @@ public function __construct(S3Client $service, $bucket, array $options = [], $de ], $options ); - - $this->detectContentType = $detectContentType; } /** * {@inheritdoc} */ - public function setMetadata($key, $metadata) + public function setMetadata(string $key, array $content): void { // BC with AmazonS3 adapter if (isset($metadata['contentType'])) { @@ -68,7 +55,7 @@ public function setMetadata($key, $metadata) /** * {@inheritdoc} */ - public function getMetadata($key) + public function getMetadata(string $key): array { return $this->metadata[$key] ?? []; } @@ -76,7 +63,7 @@ public function getMetadata($key) /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { $this->ensureBucketExists(); $options = $this->getOptions($key); @@ -100,7 +87,7 @@ public function read($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->ensureBucketExists(); $options = $this->getOptions( @@ -120,7 +107,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->ensureBucketExists(); $options = $this->getOptions($key, ['Body' => $content]); @@ -149,7 +136,7 @@ public function write($key, $content) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { return $this->service->doesObjectExist($this->bucket, $this->computePath($key)); } @@ -157,7 +144,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { try { $result = $this->service->headObject($this->getOptions($key)); @@ -171,7 +158,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function size($key) + public function size(string $key): int { try { $result = $this->service->headObject($this->getOptions($key)); @@ -182,10 +169,21 @@ public function size($key) } } + public function mimeType(string $key): string + { + try { + $result = $this->service->headObject($this->getOptions($key)); + + return ($result['ContentType']); + } catch (\Exception $e) { + return false; + } + } + /** * {@inheritdoc} */ - public function keys() + public function keys(): array { return $this->listKeys(); } @@ -193,7 +191,7 @@ public function keys() /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $this->ensureBucketExists(); @@ -216,7 +214,7 @@ public function listKeys($prefix = '') /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { try { $this->service->deleteObject($this->getOptions($key)); @@ -230,7 +228,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { $result = $this->service->listObjects([ 'Bucket' => $this->bucket, @@ -255,7 +253,7 @@ public function isDirectory($key) * @throws \RuntimeException if the bucket does not exists or could not be * created */ - protected function ensureBucketExists() + protected function ensureBucketExists(): bool { if ($this->bucketExists) { return true; @@ -281,7 +279,7 @@ protected function ensureBucketExists() return true; } - protected function getOptions($key, array $options = []) + protected function getOptions(string $key, array $options = []): array { $options['ACL'] = $this->options['acl']; $options['Bucket'] = $this->bucket; @@ -296,7 +294,7 @@ protected function getOptions($key, array $options = []) return $options; } - protected function computePath($key) + protected function computePath(string $key): string { if (empty($this->options['directory'])) { return $key; @@ -307,22 +305,13 @@ protected function computePath($key) /** * Computes the key from the specified path. - * - * @param string $path - * - * return string */ - protected function computeKey($path) + protected function computeKey(string $path): string { return ltrim(substr($path, strlen($this->options['directory'])), '/'); } - /** - * @param string $content - * - * @return string - */ - private function guessContentType($content) + private function guessContentType(mixed $content): false|string { $fileInfo = new \finfo(FILEINFO_MIME_TYPE); @@ -332,15 +321,4 @@ private function guessContentType($content) return $fileInfo->buffer($content); } - - public function mimeType($key) - { - try { - $result = $this->service->headObject($this->getOptions($key)); - - return ($result['ContentType']); - } catch (\Exception $e) { - return false; - } - } } diff --git a/src/Gaufrette/Adapter/MetadataSupporter.php b/src/Gaufrette/Adapter/MetadataSupporter.php index 19f319b5..c40f5f64 100644 --- a/src/Gaufrette/Adapter/MetadataSupporter.php +++ b/src/Gaufrette/Adapter/MetadataSupporter.php @@ -9,7 +9,7 @@ */ interface MetadataSupporter { - public function setMetadata(string $key, array $content); + public function setMetadata(string $key, array $content): void; public function getMetadata(string $key): array; }