|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phant\Client\Service; |
| 4 | + |
| 5 | +use FTP\Connection; |
| 6 | + |
| 7 | +class Ftp implements \Phant\Client\Port\Ftp |
| 8 | +{ |
| 9 | + private Connection $connection; |
| 10 | + |
| 11 | + public function __construct( |
| 12 | + string $host, |
| 13 | + string $username, |
| 14 | + string $password, |
| 15 | + int $port = 21 |
| 16 | + ) { |
| 17 | + $this->connection = $this->connect( |
| 18 | + host: $host, |
| 19 | + username: $username, |
| 20 | + password: $password, |
| 21 | + port: $port |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + private function connect(string $host, string $username, string $password, int $port): Connection |
| 26 | + { |
| 27 | + $ftp = ftp_connect($host, $port); |
| 28 | + if (!$ftp) { |
| 29 | + throw new \RuntimeException("Failed to connect to FTP server: {$host}"); |
| 30 | + } |
| 31 | + |
| 32 | + $login = ftp_login($ftp, $username, $password); |
| 33 | + if (!$login) { |
| 34 | + throw new \RuntimeException("Failed to log in to FTP server: {$host}"); |
| 35 | + } |
| 36 | + |
| 37 | + return $ftp; |
| 38 | + } |
| 39 | + |
| 40 | + public function listFiles(string $path): array |
| 41 | + { |
| 42 | + $files = ftp_nlist($this->connection, $path); |
| 43 | + if (false === $files) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + return $files; |
| 48 | + } |
| 49 | + |
| 50 | + public function download(string $remotePath, ?string $localDirectory = null): string |
| 51 | + { |
| 52 | + if ($localDirectory) { |
| 53 | + $localFilePath = rtrim($localDirectory, '/') . '/' . basename($remotePath); |
| 54 | + } else { |
| 55 | + $localFilePath = sys_get_temp_dir() . '/' . basename($remotePath); |
| 56 | + } |
| 57 | + |
| 58 | + if (!ftp_get($this->connection, $localFilePath, $remotePath, FTP_BINARY)) { |
| 59 | + throw new \RuntimeException("Failed to download file from FTP: {$remotePath}"); |
| 60 | + } |
| 61 | + |
| 62 | + return $localFilePath; |
| 63 | + } |
| 64 | + |
| 65 | + public function delete(string $path): bool |
| 66 | + { |
| 67 | + if (!ftp_delete($this->connection, $path)) { |
| 68 | + throw new \RuntimeException("Failed to delete file from FTP: {$path}"); |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + public function __destruct() |
| 75 | + { |
| 76 | + ftp_close($this->connection); |
| 77 | + } |
| 78 | +} |
0 commit comments