Skip to content

Commit d598148

Browse files
committed
✨ Adds a FTP client
1 parent 17bf74d commit d598148

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ test/coverage
1010
.DS_Store
1111
.nova/*
1212
.php-cs-fixer.cache
13+
docker/

component/Port/Ftp.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phant\Client\Port;
6+
7+
interface Ftp
8+
{
9+
public function listFiles(string $path): array;
10+
11+
/**
12+
* @param string $remotePath The path of the file on the FTP server
13+
* @param string|null $localDirectory The local directory to save the downloaded file. If null, a default temp directory will be used.
14+
* @return string Local file path of the downloaded file
15+
*
16+
* @throws \RuntimeException
17+
*/
18+
public function download(string $remotePath, ?string $localDirectory = null): string;
19+
20+
public function delete(string $remotePath): bool;
21+
}

component/Service/Ftp.php

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

component/Service/MySQL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected function getPdo(
120120
if ($this->pdo) {
121121
return $this->pdo;
122122
}
123-
123+
124124
$this->pdo = new PDO(
125125
'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ';port=' . $this->port . ';charset=' . $this->charset,
126126
$this->user,

0 commit comments

Comments
 (0)