Skip to content

Commit

Permalink
Type AzureBlobStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Apr 21, 2023
1 parent a7db6fd commit 7ef46f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 114 deletions.
142 changes: 33 additions & 109 deletions src/Gaufrette/Adapter/AzureBlobStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,67 +28,30 @@ class AzureBlobStorage implements Adapter, MetadataSupporter, SizeCalculator, Ch
const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists';
const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound';

/**
* @var AzureBlobStorage\BlobProxyFactoryInterface
*/
protected $blobProxyFactory;
protected \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy;

/**
* @var string
*/
protected $containerName;
protected bool $multiContainerMode = false;

/**
* @var bool
*/
protected $detectContentType;
protected CreateContainerOptions $createContainerOptions;

/**
* @var \MicrosoftAzure\Storage\Blob\Internal\IBlob
*/
protected $blobProxy;

/**
* @var bool
*/
protected $multiContainerMode = false;

/**
* @var CreateContainerOptions
*/
protected $createContainerOptions;

/**
* @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param string|null $containerName
* @param bool $create
* @param bool $detectContentType
*
* @throws \RuntimeException
*/
public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true)
{
$this->blobProxyFactory = $blobProxyFactory;
$this->containerName = $containerName;
$this->detectContentType = $detectContentType;
public function __construct(
private readonly BlobProxyFactoryInterface $blobProxyFactory,
private readonly ?string $containerName = null,
bool $create = false,
private readonly bool $detectContentType = true
) {
if (null === $containerName) {
$this->multiContainerMode = true;
} elseif ($create) {
$this->createContainer($containerName);
}
}

/**
* @return CreateContainerOptions
*/
public function getCreateContainerOptions()
public function getCreateContainerOptions(): CreateContainerOptions
{
return $this->createContainerOptions;
}

/**
* @param CreateContainerOptions $options
*/
public function setCreateContainerOptions(CreateContainerOptions $options)
{
$this->createContainerOptions = $options;
Expand All @@ -97,12 +60,9 @@ public function setCreateContainerOptions(CreateContainerOptions $options)
/**
* Creates a new container.
*
* @param string $containerName
* @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options
*
* @throws \RuntimeException if cannot create the container
*/
public function createContainer($containerName, CreateContainerOptions $options = null)
public function createContainer(string $containerName, CreateContainerOptions $options = null)
{
$this->init();

Expand All @@ -129,12 +89,9 @@ public function createContainer($containerName, CreateContainerOptions $options
/**
* Deletes a container.
*
* @param string $containerName
* @param BlobServiceOptions $options
*
* @throws \RuntimeException if cannot delete the container
*/
public function deleteContainer($containerName, BlobServiceOptions $options = null)
public function deleteContainer(string $containerName, BlobServiceOptions $options = null)
{
$this->init();

Expand All @@ -156,10 +113,8 @@ public function deleteContainer($containerName, BlobServiceOptions $options = nu

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function read($key)
public function read(string $key): string|bool
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -177,10 +132,8 @@ public function read($key)

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function write($key, $content)
public function write(string $key, mixed $content): int|bool
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand Down Expand Up @@ -220,10 +173,8 @@ public function write($key, $content)

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function exists($key)
public function exists(string $key): bool
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand Down Expand Up @@ -260,9 +211,8 @@ public function exists($key)

/**
* {@inheritdoc}
* @throws \RuntimeException
*/
public function keys()
public function keys(): array
{
$this->init();

Expand Down Expand Up @@ -296,10 +246,8 @@ function (Container $container) {

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -318,7 +266,7 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function size($key)
public function size(string $key): int
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -337,7 +285,7 @@ public function size($key)
/**
* {@inheritdoc}
*/
public function mimeType($key)
public function mimeType(string $key): string
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -356,7 +304,7 @@ public function mimeType($key)
/**
* {@inheritdoc}
*/
public function checksum($key)
public function checksum(string $key): string
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -375,10 +323,8 @@ public function checksum($key)

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function delete($key)
public function delete(string $key): bool
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -396,10 +342,8 @@ public function delete($key)

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function rename($sourceKey, $targetKey)
public function rename(string $sourceKey, string $targetKey): bool
{
$this->init();

Expand All @@ -424,18 +368,16 @@ public function rename($sourceKey, $targetKey)
/**
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
// Windows Azure Blob Storage does not support directories
return false;
}

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function setMetadata($key, $content)
public function setMetadata(string $key, array $content): void
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -457,10 +399,8 @@ public function setMetadata($key, $content)

/**
* {@inheritdoc}
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function getMetadata($key)
public function getMetadata(string $key): array
{
$this->init();
list($containerName, $key) = $this->tokenizeKey($key);
Expand All @@ -485,7 +425,7 @@ public function getMetadata($key)
/**
* Lazy initialization, automatically called when some method is called after construction.
*/
protected function init()
protected function init(): void
{
if ($this->blobProxy === null) {
$this->blobProxy = $this->blobProxyFactory->create();
Expand All @@ -495,13 +435,9 @@ protected function init()
/**
* Throws a runtime exception if a give ServiceException derived from a "container not found" error.
*
* @param ServiceException $exception
* @param string $action
* @param string $containerName
*
* @throws \RuntimeException
*/
protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName)
protected function failIfContainerNotFound(ServiceException $exception, string $action, string $containerName): void
{
$errorCode = $this->getErrorCodeFromServiceException($exception);

Expand All @@ -516,12 +452,8 @@ protected function failIfContainerNotFound(ServiceException $exception, $action,

/**
* Extracts the error code from a service exception.
*
* @param ServiceException $exception
*
* @return string
*/
protected function getErrorCodeFromServiceException(ServiceException $exception)
protected function getErrorCodeFromServiceException(ServiceException $exception): string
{
$xml = @simplexml_load_string($exception->getResponse()->getBody());

Expand All @@ -534,10 +466,8 @@ protected function getErrorCodeFromServiceException(ServiceException $exception)

/**
* @param string|resource $content
*
* @return string
*/
private function guessContentType($content)
private function guessContentType($content): string
{
$fileInfo = new \finfo(FILEINFO_MIME_TYPE);

Expand All @@ -549,12 +479,9 @@ private function guessContentType($content)
}

/**
* @param string $key
*
* @return array
* @throws \InvalidArgumentException
* @return array<string, string>
*/
private function tokenizeKey($key)
private function tokenizeKey(string $key): array
{
$containerName = $this->containerName;
if (false === $this->multiContainerMode) {
Expand All @@ -574,12 +501,9 @@ private function tokenizeKey($key)
}

/**
* @param string $containerName
* @param null $prefix
*
* @return array
* @return array<string>
*/
private function fetchBlobs($containerName, $prefix = null)
private function fetchBlobs(string $containerName, ?string $prefix = null): array
{
$blobList = $this->blobProxy->listBlobs($containerName);

Expand Down
6 changes: 1 addition & 5 deletions src/Gaufrette/Adapter/ChecksumCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ interface ChecksumCalculator
{
/**
* Returns the checksum of the specified key.
*
* @param string $key
*
* @return string
*/
public function checksum($key);
public function checksum(string $key): string;
}

0 comments on commit 7ef46f7

Please sign in to comment.