Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.22% |
13 / 18 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SiteCommand | |
72.22% |
13 / 18 |
|
66.67% |
2 / 3 |
8.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| run | |
61.54% |
8 / 13 |
|
0.00% |
0 / 1 |
4.91 | |||
| create | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\FtpServer\Commands; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\FtpServer\SiteCommands\IdleCommand; |
| 6 | use Apie\FtpServer\SiteCommands\SiteCommandInterface; |
| 7 | use React\Socket\ConnectionInterface; |
| 8 | |
| 9 | class SiteCommand implements CommandInterface |
| 10 | { |
| 11 | private array $siteCommands; |
| 12 | |
| 13 | public function __construct(SiteCommandInterface... $siteCommands) |
| 14 | { |
| 15 | $this->siteCommands = []; |
| 16 | foreach ($siteCommands as $siteCommand) { |
| 17 | $this->siteCommands[strtoupper($siteCommand->getName())] = $siteCommand; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | public function run(ApieContext $apieContext, string $arg = ''): ApieContext |
| 22 | { |
| 23 | $conn = $apieContext->getContext(ConnectionInterface::class); |
| 24 | list($command, $args) = array_pad(explode(' ', $arg, 2), 2, null); |
| 25 | $command = strtoupper($command); |
| 26 | if ($command === 'HELP') { |
| 27 | // Iterate all registered SITE commands and print their help text. |
| 28 | // Use a 211 reply (system status) or 200-style single-line reply. We'll send multiple lines. |
| 29 | foreach ($this->siteCommands as $name => $siteCmd) { |
| 30 | // Each site command implements getHelpText() |
| 31 | $conn->write(sprintf("214-%s %s\r\n", $name, $siteCmd->getHelpText())); |
| 32 | } |
| 33 | // End of help list |
| 34 | $conn->write("214 End of SITE HELP list\r\n"); |
| 35 | return $apieContext; |
| 36 | } |
| 37 | if (isset($this->siteCommands[$command])) { |
| 38 | $siteCommand = $this->siteCommands[$command]; |
| 39 | return $siteCommand->run($apieContext, $args ?? ''); |
| 40 | } |
| 41 | $conn->write("502 Command not implemented\r\n"); |
| 42 | return $apieContext; |
| 43 | } |
| 44 | |
| 45 | public static function create(SiteCommandInterface... $siteCommands): self |
| 46 | { |
| 47 | $siteCommands[] = new IdleCommand(); |
| 48 | return new self(...$siteCommands); |
| 49 | } |
| 50 | } |