Skip to content

Commit

Permalink
refacto: type filesystem and filesystem interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Oct 30, 2023
1 parent 0cb8b54 commit 1a4e105
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 93 deletions.
61 changes: 23 additions & 38 deletions src/Gaufrette/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
*/
class Filesystem implements FilesystemInterface
{
protected $adapter;
protected Adapter $adapter;

/**
* Contains File objects created with $this->createFile() method.
*
* @var array
*/
protected $fileRegister = [];
protected array $fileRegister = [];

/**
* @param Adapter $adapter A configured Adapter instance
Expand All @@ -29,20 +29,15 @@ public function __construct(Adapter $adapter)
$this->adapter = $adapter;
}

/**
* Returns the adapter.
*
* @return Adapter
*/
public function getAdapter()
public function getAdapter(): Adapter
{
return $this->adapter;
}

/**
* {@inheritdoc}
*/
public function has($key)
public function has(string $key): bool
{
self::assertValidKey($key);

Expand All @@ -52,7 +47,7 @@ public function has($key)
/**
* {@inheritdoc}
*/
public function rename($sourceKey, $targetKey)
public function rename(string $sourceKey, string $targetKey): bool
{
self::assertValidKey($sourceKey);
self::assertValidKey($targetKey);
Expand All @@ -78,7 +73,7 @@ public function rename($sourceKey, $targetKey)
/**
* {@inheritdoc}
*/
public function get($key, $create = false)
public function get(string $key, bool $create = false): File
{
self::assertValidKey($key);

Expand All @@ -92,7 +87,7 @@ public function get($key, $create = false)
/**
* {@inheritdoc}
*/
public function write($key, $content, $overwrite = false)
public function write(string $key, string $content, bool $overwrite = false): int
{
self::assertValidKey($key);

Expand All @@ -112,7 +107,7 @@ public function write($key, $content, $overwrite = false)
/**
* {@inheritdoc}
*/
public function read($key)
public function read(string $key): string
{
self::assertValidKey($key);

Expand All @@ -130,7 +125,7 @@ public function read($key)
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
self::assertValidKey($key);

Expand All @@ -148,15 +143,15 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function keys()
public function keys(): array
{
return $this->adapter->keys();
}

/**
* {@inheritdoc}
*/
public function listKeys($prefix = '')
public function listKeys(string $prefix = ''): array
{
if ($this->adapter instanceof ListKeysAware) {
return $this->adapter->listKeys($prefix);
Expand Down Expand Up @@ -184,7 +179,7 @@ public function listKeys($prefix = '')
/**
* {@inheritdoc}
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
self::assertValidKey($key);

Expand All @@ -196,7 +191,7 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function checksum($key)
public function checksum(string $key): string
{
self::assertValidKey($key);

Expand All @@ -212,7 +207,7 @@ public function checksum($key)
/**
* {@inheritdoc}
*/
public function size($key)
public function size(string $key): int
{
self::assertValidKey($key);

Expand All @@ -228,7 +223,7 @@ public function size($key)
/**
* {@inheritdoc}
*/
public function createStream($key)
public function createStream(string $key): Stream|Stream\InMemoryBuffer
{
self::assertValidKey($key);

Expand All @@ -242,7 +237,7 @@ public function createStream($key)
/**
* {@inheritdoc}
*/
public function createFile($key)
public function createFile(string $key): File
{
self::assertValidKey($key);

Expand All @@ -260,7 +255,7 @@ public function createFile($key)
/**
* {@inheritdoc}
*/
public function mimeType($key)
public function mimeType(string $key): string|bool
{
self::assertValidKey($key);

Expand All @@ -282,11 +277,9 @@ public function mimeType($key)
* Key must be non empty string, otherwise it will throw Exception\FileNotFound
* {@see http://php.net/manual/en/function.empty.php}
*
* @param string $key
*
* @throws Exception\FileNotFound when sourceKey does not exist
*/
private function assertHasFile($key)
private function assertHasFile(string $key): void
{
if (!$this->has($key)) {
throw new Exception\FileNotFound($key);
Expand All @@ -295,30 +288,24 @@ private function assertHasFile($key)

/**
* Checks if matching File object by given key exists in the fileRegister.
*
* @param string $key
*
* @return bool
*/
private function isFileInRegister($key)
private function isFileInRegister(string $key): bool
{
return array_key_exists($key, $this->fileRegister);
}

/**
* Clear files register.
*/
public function clearFileRegister()
public function clearFileRegister(): void
{
$this->fileRegister = [];
}

/**
* Removes File object from register.
*
* @param string $key
*/
public function removeFromRegister($key)
public function removeFromRegister(string $key): void
{
if ($this->isFileInRegister($key)) {
unset($this->fileRegister[$key]);
Expand All @@ -328,17 +315,15 @@ public function removeFromRegister($key)
/**
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
return $this->adapter->isDirectory($key);
}

/**
* @param string $key
*
* @throws \InvalidArgumentException Given $key should not be empty
*/
private static function assertValidKey($key)
private static function assertValidKey(string $key): void
{
if (empty($key)) {
throw new \InvalidArgumentException('Object path is empty.');
Expand Down
70 changes: 15 additions & 55 deletions src/Gaufrette/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@ interface FilesystemInterface
/**
* Indicates whether the file matching the specified key exists.
*
* @param string $key
*
* @return bool TRUE if the file exists, FALSE otherwise
*
* @throws \InvalidArgumentException If $key is invalid
*/
public function has($key);
public function has(string $key): bool;

/**
* Renames a file.
*
* File::rename should be preferred or you may face bad filesystem consistency.
*
* @param string $sourceKey
* @param string $targetKey
*
* @return bool TRUE if the rename was successful
*
* @throws Exception\FileNotFound when sourceKey does not exist
Expand All @@ -32,7 +27,7 @@ public function has($key);
*
* @see File::rename()
*/
public function rename($sourceKey, $targetKey);
public function rename(string $sourceKey, string $targetKey): bool;

/**
* Returns the file matching the specified key.
Expand All @@ -45,7 +40,7 @@ public function rename($sourceKey, $targetKey);
*
* @return File
*/
public function get($key, $create = false);
public function get(string $key, bool $create = false): File;

/**
* Writes the given content into the file.
Expand All @@ -60,7 +55,7 @@ public function get($key, $create = false);
*
* @return int The number of bytes that were written into the file
*/
public function write($key, $content, $overwrite = false);
public function write(string $key, string $content, bool $overwrite = false): int;

/**
* Reads the content from the file.
Expand All @@ -70,27 +65,19 @@ public function write($key, $content, $overwrite = false);
* @throws Exception\FileNotFound when file does not exist
* @throws \RuntimeException when cannot read file
* @throws \InvalidArgumentException If $key is invalid
*
* @return string
*/
public function read($key);
public function read(string $key): string;

/**
* Deletes the file matching the specified key.
*
* @param string $key
*
* @throws \RuntimeException when cannot read file
* @throws \InvalidArgumentException If $key is invalid
*
* @return bool
*/
public function delete($key);
public function delete(string $key): bool;

/**
* Returns an array of all keys.
*
* @return array
*/
public function keys();

Expand All @@ -100,83 +87,56 @@ public function keys();
*
* if adapter implements ListKeysAware interface, adapter's implementation will be used,
* in not, ALL keys will be requested and iterated through.
*
* @param string $prefix
*
* @return array
*/
public function listKeys($prefix = '');
public function listKeys(string $prefix = ''): array;

/**
* Returns the last modified time of the specified file.
*
* @param string $key
*
* @return int An UNIX like timestamp
* @return int|bool An UNIX like timestamp or false
*
* @throws \InvalidArgumentException If $key is invalid
*/
public function mtime($key);
public function mtime(string $key): int|bool;

/**
* Returns the checksum of the specified file's content.
*
* @param string $key
*
* @return string A MD5 hash
*
* @throws \InvalidArgumentException If $key is invalid
*/
public function checksum($key);
public function checksum(string $key): string;

/**
* Returns the size of the specified file's content.
*
* @param string $key
*
* @return int File size in Bytes
*
* @throws \InvalidArgumentException If $key is invalid
*/
public function size($key);
public function size(string $key): int;

/**
* Gets a new stream instance of the specified file.
*
* @param $key
*
* @return Stream|Stream\InMemoryBuffer
*
* @throws \InvalidArgumentException If $key is invalid
*/
public function createStream($key);
public function createStream(string $key): Stream|Stream\InMemoryBuffer;

/**
* Creates a new file in a filesystem.
*
* @param $key
*
* @return File
*
* @throws \InvalidArgumentException If $key is invalid
*/
public function createFile($key);
public function createFile(string $key): File;

/**
* Get the mime type of the provided key.
*
* @param string $key
*
* @return string|false
*
* @throws \InvalidArgumentException If $key is invalid
*/
public function mimeType($key);
public function mimeType(string $key): string|bool;

/**
* @param string $key
*
* @return bool
*/
public function isDirectory($key);
public function isDirectory(string $key): bool;
}

0 comments on commit 1a4e105

Please sign in to comment.