Skip to content

Commit

Permalink
Type AsyncAwsS3
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Apr 21, 2023
1 parent ab3604b commit ed22632
Showing 1 changed file with 30 additions and 53 deletions.
83 changes: 30 additions & 53 deletions src/Gaufrette/Adapter/AsyncAwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,26 @@
/**
* Amazon S3 adapter using the AsyncAws.
*
* @author Michael Dowling <[email protected]>
* @author Michael Dowling <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
class AsyncAwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider
{
/** @var SimpleS3Client */
protected $service;
/** @var string */
protected $bucket;
/** @var array */
protected $options;
/** @var bool */
protected $bucketExists;
/** @var array */
protected $metadata = [];
/** @var bool */
protected $detectContentType;

/**
* @param SimpleS3Client $service
* @param string $bucket
* @param array $options
* @param bool $detectContentType
*/
public function __construct(SimpleS3Client $service, $bucket, array $options = [], $detectContentType = false)
{
protected SimpleS3Client $service;
protected array $options;
protected bool $bucketExists;
protected array $metadata = [];

public function __construct(
SimpleS3Client $service,
private readonly string $bucket,
array $options = [],
private readonly bool $detectContentType = false
) {
if (!class_exists(SimpleS3Client::class)) {
throw new \LogicException('You need to install package "async-aws/simple-s3" to use this adapter');
}
$this->service = $service;
$this->bucket = $bucket;
$this->options = array_replace(
[
'create' => false,
Expand All @@ -49,14 +38,12 @@ public function __construct(SimpleS3Client $service, $bucket, array $options = [
],
$options
);

$this->detectContentType = $detectContentType;
}

/**
* {@inheritdoc}
*/
public function setMetadata($key, $content)
public function setMetadata(string $key, array $content)
{
// BC with AmazonS3 adapter
if (isset($content['contentType'])) {
Expand All @@ -70,15 +57,15 @@ public function setMetadata($key, $content)
/**
* {@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 @@ -102,7 +89,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 @@ -121,9 +108,8 @@ public function rename($sourceKey, $targetKey)

/**
* {@inheritdoc}
* @param string|resource $content
*/
public function write($key, $content)
public function write(string $key, mixed $content): int|bool
{
$this->ensureBucketExists();
$options = $this->getOptions($key);
Expand Down Expand Up @@ -153,15 +139,15 @@ public function write($key, $content)
/**
* {@inheritdoc}
*/
public function exists($key)
public function exists(string $key): bool
{
return $this->service->has($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 @@ -175,14 +161,14 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function size($key)
public function size(string $key): int
{
$result = $this->service->headObject($this->getOptions($key));

return (int) $result->getContentLength();
}

public function mimeType($key)
public function mimeType(string $key): string
{
$result = $this->service->headObject($this->getOptions($key));

Expand All @@ -192,15 +178,15 @@ public function mimeType($key)
/**
* {@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 @@ -223,7 +209,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 @@ -237,7 +223,7 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
$result = $this->service->listObjectsV2([
'Bucket' => $this->bucket,
Expand All @@ -261,7 +247,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 @@ -288,7 +274,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 @@ -303,7 +289,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 @@ -314,22 +300,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|resource $content
*
* @return string
*/
private function guessContentType($content)
private function guessContentType(mixed $content): false|string
{
$fileInfo = new \finfo(FILEINFO_MIME_TYPE);

Expand Down

0 comments on commit ed22632

Please sign in to comment.