From 7ef46f71451c1084b36a4059527dcbe9af70c244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin?= Date: Fri, 21 Apr 2023 15:56:53 +0200 Subject: [PATCH] Type AzureBlobStorage --- src/Gaufrette/Adapter/AzureBlobStorage.php | 142 +++++-------------- src/Gaufrette/Adapter/ChecksumCalculator.php | 6 +- 2 files changed, 34 insertions(+), 114 deletions(-) diff --git a/src/Gaufrette/Adapter/AzureBlobStorage.php b/src/Gaufrette/Adapter/AzureBlobStorage.php index c153f9c01..d9d81c329 100644 --- a/src/Gaufrette/Adapter/AzureBlobStorage.php +++ b/src/Gaufrette/Adapter/AzureBlobStorage.php @@ -28,49 +28,18 @@ 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) { @@ -78,17 +47,11 @@ public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $contai } } - /** - * @return CreateContainerOptions - */ - public function getCreateContainerOptions() + public function getCreateContainerOptions(): CreateContainerOptions { return $this->createContainerOptions; } - /** - * @param CreateContainerOptions $options - */ public function setCreateContainerOptions(CreateContainerOptions $options) { $this->createContainerOptions = $options; @@ -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(); @@ -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(); @@ -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); @@ -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); @@ -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); @@ -260,9 +211,8 @@ public function exists($key) /** * {@inheritdoc} - * @throws \RuntimeException */ - public function keys() + public function keys(): array { $this->init(); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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(); @@ -424,7 +368,7 @@ 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; @@ -432,10 +376,8 @@ public function isDirectory($key) /** * {@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); @@ -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); @@ -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(); @@ -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); @@ -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()); @@ -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); @@ -549,12 +479,9 @@ private function guessContentType($content) } /** - * @param string $key - * - * @return array - * @throws \InvalidArgumentException + * @return array */ - private function tokenizeKey($key) + private function tokenizeKey(string $key): array { $containerName = $this->containerName; if (false === $this->multiContainerMode) { @@ -574,12 +501,9 @@ private function tokenizeKey($key) } /** - * @param string $containerName - * @param null $prefix - * - * @return array + * @return array */ - private function fetchBlobs($containerName, $prefix = null) + private function fetchBlobs(string $containerName, ?string $prefix = null): array { $blobList = $this->blobProxy->listBlobs($containerName); diff --git a/src/Gaufrette/Adapter/ChecksumCalculator.php b/src/Gaufrette/Adapter/ChecksumCalculator.php index 46ca2b7db..cef02e127 100644 --- a/src/Gaufrette/Adapter/ChecksumCalculator.php +++ b/src/Gaufrette/Adapter/ChecksumCalculator.php @@ -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; }