Skip to content

Commit

Permalink
Type PhpseclibSftp
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Apr 24, 2023
1 parent 79eda31 commit b05376b
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/Gaufrette/Adapter/PhpseclibSftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,35 @@

class PhpseclibSftp implements Adapter, FileFactory, ListKeysAware
{
protected $sftp;
protected $directory;
protected $create;
protected $initialized = false;
protected bool $initialized = false;

/**
* @param SecLibSFTP $sftp An Sftp instance
* @param string $directory The distant directory
* @param bool $create Whether to create the remote directory if it
* does not exist
*/
public function __construct(SecLibSFTP $sftp, $directory = null, $create = false)
{
public function __construct(
private readonly SecLibSFTP $sftp,
private readonly ?string $directory = null,
private readonly bool $create = false
) {
if (!class_exists(SecLibSFTP::class)) {
throw new \LogicException('You need to install package "phpseclib/phpseclib" to use this adapter');
}
$this->sftp = $sftp;
$this->directory = $directory;
$this->create = $create;
}

/**
* {@inheritdoc}
*/
public function read($key)
public function read(string $key): string|bool
{
return $this->sftp->get($this->computePath($key));
}

/**
* {@inheritdoc}
*/
public function rename($sourceKey, $targetKey)
public function rename(string $sourceKey, string $targetKey): bool
{
$this->initialize();

Expand All @@ -56,7 +52,7 @@ public function rename($sourceKey, $targetKey)
/**
* {@inheritdoc}
*/
public function write($key, $content)
public function write(string $key, mixed $content): int|bool
{
$this->initialize();

Expand All @@ -72,7 +68,7 @@ public function write($key, $content)
/**
* {@inheritdoc}
*/
public function exists($key)
public function exists(string $key): bool
{
$this->initialize();

Expand All @@ -82,7 +78,7 @@ public function exists($key)
/**
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
$this->initialize();

Expand All @@ -99,7 +95,7 @@ public function isDirectory($key)
/**
* {@inheritdoc}
*/
public function keys()
public function keys(): array
{
$keys = $this->fetchKeys();

Expand All @@ -109,7 +105,7 @@ public function keys()
/**
* {@inheritdoc}
*/
public function listKeys($prefix = '')
public function listKeys(string $prefix = ''): array
{
preg_match('/(.*?)[^\/]*$/', $prefix, $match);
$directory = rtrim($match[1], '/');
Expand All @@ -136,7 +132,7 @@ public function listKeys($prefix = '')
/**
* {@inheritdoc}
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
$this->initialize();

Expand All @@ -148,15 +144,15 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
return $this->sftp->delete($this->computePath($key), false);
}

/**
* {@inheritdoc}
*/
public function createFile($key, Filesystem $filesystem)
public function createFile(string $key, Filesystem $filesystem): File
{
$file = new File($key, $filesystem);

Expand All @@ -173,7 +169,8 @@ public function createFile($key, Filesystem $filesystem)
*
* It will ensure the root directory exists
*/
protected function initialize()

protected function initialize(): void
{
if ($this->initialized) {
return;
Expand All @@ -184,7 +181,7 @@ protected function initialize()
$this->initialized = true;
}

protected function ensureDirectoryExists($directory, $create)
protected function ensureDirectoryExists(string $directory, bool $create)
{
$pwd = $this->sftp->pwd();
if ($this->sftp->chdir($directory)) {
Expand All @@ -198,12 +195,15 @@ protected function ensureDirectoryExists($directory, $create)
}
}

protected function computePath($key)
protected function computePath(string $key): string
{
return $this->directory . '/' . ltrim($key, '/');
}

protected function fetchKeys($directory = '', $onlyKeys = true)
/**
* @return array<string, array<string>>
*/
protected function fetchKeys(string $directory = '', bool $onlyKeys = true): array
{
$keys = ['keys' => [], 'dirs' => []];
$computedPath = $this->computePath($directory);
Expand Down

0 comments on commit b05376b

Please sign in to comment.