Skip to content

Commit

Permalink
Type AwsS3
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Apr 21, 2023
1 parent ed22632 commit 072b450
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/Gaufrette/Adapter/AsyncAwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
100 changes: 39 additions & 61 deletions src/Gaufrette/Adapter/AwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'])) {
Expand All @@ -68,15 +55,15 @@ public function setMetadata($key, $metadata)
/**
* {@inheritdoc}
*/
public function getMetadata($key)
public function getMetadata(string $key): array
{
return $this->metadata[$key] ?? [];
}

/**
* {@inheritdoc}
*/
public function read($key)
public function read(string $key): string|bool
{
$this->ensureBucketExists();
$options = $this->getOptions($key);
Expand All @@ -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(
Expand All @@ -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]);
Expand Down Expand Up @@ -149,15 +136,15 @@ 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));
}

/**
* {@inheritdoc}
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
try {
$result = $this->service->headObject($this->getOptions($key));
Expand All @@ -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));
Expand All @@ -182,18 +169,29 @@ 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();
}

/**
* {@inheritdoc}
*/
public function listKeys($prefix = '')
public function listKeys(string $prefix = ''): array
{
$this->ensureBucketExists();

Expand All @@ -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));
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/Gaufrette/Adapter/MetadataSupporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 072b450

Please sign in to comment.