Created
April 9, 2019 08:44
-
-
Save vertisan/e1195395ad64b31eda6fe32a4a4a8634 to your computer and use it in GitHub Desktop.
DI Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Service\RemoteServer; | |
use League\Flysystem\FileExistsException; | |
use League\Flysystem\FileNotFoundException; | |
use League\Flysystem\FilesystemInterface; | |
abstract class RemoteServerAbstract implements RemoteServerInterface | |
{ | |
/** | |
* @var string | |
*/ | |
protected $host; | |
/** | |
* @var string | |
*/ | |
protected $username; | |
/** | |
* @var string | |
*/ | |
protected $password; | |
/** | |
* @var int|null | |
*/ | |
protected $port; | |
/** | |
* @var string | |
*/ | |
protected $privateKey; | |
/** | |
* @var FilesystemInterface | |
*/ | |
protected $filesystem; | |
/** | |
* @param string $host | |
*/ | |
public function setHost(string $host): void | |
{ | |
$this->host = $host; | |
} | |
/** | |
* @param string $username | |
*/ | |
public function setUsername(string $username): void | |
{ | |
$this->username = $username; | |
} | |
/** | |
* @param string $password | |
*/ | |
public function setPassword(string $password): void | |
{ | |
$this->password = $password; | |
} | |
/** | |
* @param int $port | |
*/ | |
public function setPort(int $port): void | |
{ | |
$this->port = $port; | |
} | |
/** | |
* @param string $privateKey | |
*/ | |
public function setPrivateKey(string $privateKey): void | |
{ | |
$this->privateKey = $privateKey; | |
} | |
/** | |
* Connect to server | |
* | |
* @return bool | |
*/ | |
abstract public function connect(): bool; | |
/** | |
* Upload file to server | |
* | |
* @param string $localFilePath | |
* @param string $destinationPath | |
* @param string|null $newFileName | |
* @return bool | |
* @throws FileExistsException | |
* @throws FileNotFoundException | |
*/ | |
public function upload(string $localFilePath, string $destinationPath, ?string $newFileName = null): bool | |
{ | |
$stream = fopen($localFilePath, 'rb+'); | |
$fileName = basename($localFilePath); | |
if ($newFileName) { | |
$fileName = $newFileName; | |
} | |
$remoteFilePath = sprintf('%s/%s', $destinationPath, $fileName); | |
if ($this->filesystem->has($remoteFilePath)) { | |
return $this->filesystem->updateStream( | |
$remoteFilePath, | |
$stream | |
); | |
} | |
return $this->filesystem->writeStream( | |
$remoteFilePath, | |
$stream | |
); | |
} | |
/** | |
* Check for server connection | |
* | |
* @return bool | |
*/ | |
public function checkConnection(): bool | |
{ | |
try { | |
$this->filesystem->listContents($this->rootPath); | |
} catch (\ErrorException $exception) { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Service\RemoteServer\Protocol; | |
use App\Service\RemoteServer\AbstractConnector; | |
use League\Flysystem\Adapter\Ftp as Adapter; | |
use League\Flysystem\Filesystem; | |
use RuntimeException; | |
class RemoteServerFTP extends RemoteServerAbstract | |
{ | |
public function __construct() | |
{ | |
$this->port = 21; | |
} | |
/** | |
* Connect to server | |
* | |
* @return bool | |
*/ | |
public function connect(): bool | |
{ | |
$this->filesystem = new Filesystem(new Adapter([ | |
'host' => $this->host, | |
'username' => $this->username, | |
'password' => $this->password, | |
'port' => $this->port, | |
])); | |
try { | |
return $this->checkConnection(); | |
} catch (RuntimeException $exception) { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Service\RemoteServer; | |
interface RemoteServerInterface | |
{ | |
public function connect(): bool; | |
public function upload(string $localFilePath, string $destinationPath, ?string $newFileName = null): bool; | |
public function checkConnection(): bool; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Service\RemoteServer\Protocol; | |
use App\Service\RemoteServer\AbstractConnector; | |
use League\Flysystem\Sftp\SftpAdapter; | |
use League\Flysystem\Filesystem; | |
use RuntimeException; | |
class RemoteServerSFTP extends RemoteServerAbstract | |
{ | |
public function __construct() | |
{ | |
$this->port = 22; | |
} | |
/** | |
* Connect to server | |
* | |
* @return bool | |
*/ | |
public function connect(): bool | |
{ | |
$this->filesystem = new Filesystem(new SftpAdapter([ | |
'host' => $this->host, | |
'username' => $this->username, | |
'password' => $this->password, | |
'port' => $this->port, | |
'privateKey' => $this->privateKey, | |
])); | |
try { | |
return $this->checkConnection(); | |
} catch (RuntimeException $exception) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment