Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
77.78% |
21 / 27 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FtpServerRunner | |
77.78% |
21 / 27 |
|
66.67% |
2 / 3 |
4.18 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
1 | |||
| run | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\FtpServer; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\FtpServer\Commands\CdupCommand; |
| 6 | use Apie\FtpServer\Commands\CwdCommand; |
| 7 | use Apie\FtpServer\Commands\EprtCommand; |
| 8 | use Apie\FtpServer\Commands\EpsvCommand; |
| 9 | use Apie\FtpServer\Commands\ListCommand; |
| 10 | use Apie\FtpServer\Commands\NlstCommand; |
| 11 | use Apie\FtpServer\Commands\PassCommand; |
| 12 | use Apie\FtpServer\Commands\PasvCommand; |
| 13 | use Apie\FtpServer\Commands\PortCommand; |
| 14 | use Apie\FtpServer\Commands\PwdCommand; |
| 15 | use Apie\FtpServer\Commands\QuitCommand; |
| 16 | use Apie\FtpServer\Commands\RetrCommand; |
| 17 | use Apie\FtpServer\Commands\SiteCommand; |
| 18 | use Apie\FtpServer\Commands\SystCommand; |
| 19 | use Apie\FtpServer\Commands\TypeCommand; |
| 20 | use Apie\FtpServer\Commands\UserCommand; |
| 21 | use Apie\FtpServer\Lists\CommandHashmap; |
| 22 | use Apie\FtpServer\SiteCommands\SiteCommandInterface; |
| 23 | use React\Socket\ConnectionInterface; |
| 24 | |
| 25 | class FtpServerRunner |
| 26 | { |
| 27 | public function __construct( |
| 28 | private readonly CommandHashmap $commands |
| 29 | ) { |
| 30 | } |
| 31 | |
| 32 | public static function create(SiteCommandInterface... $siteCommands): self |
| 33 | { |
| 34 | return new self( |
| 35 | new CommandHashmap([ |
| 36 | 'CDUP' => new CdupCommand(), |
| 37 | 'CWD' => new CwdCommand(), |
| 38 | 'EPRT' => new EprtCommand(), |
| 39 | 'EPSV' => new EpsvCommand(), |
| 40 | 'LIST' => new ListCommand(), |
| 41 | 'NLST' => new NlstCommand(), |
| 42 | 'PASS' => new PassCommand(), |
| 43 | 'PASV' => new PasvCommand(), |
| 44 | 'PORT' => new PortCommand(), |
| 45 | 'PWD' => new PwdCommand(), |
| 46 | 'QUIT' => new QuitCommand(), |
| 47 | 'RETR' => new RetrCommand(), |
| 48 | 'SITE' => SiteCommand::create(...$siteCommands), |
| 49 | 'SYST' => new SystCommand(), |
| 50 | 'TYPE' => new TypeCommand(), |
| 51 | 'USER' => new UserCommand(), |
| 52 | ]) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | public function run(ApieContext $apieContext, string $command, string $arguments = ''): ApieContext |
| 57 | { |
| 58 | if (!isset($this->commands[$command])) { |
| 59 | $apieContext->getContext(ConnectionInterface::class)->write("502 Command not implemented\r\n"); |
| 60 | error_log("Unknown command " . $command); |
| 61 | return $apieContext; |
| 62 | } |
| 63 | $commandExecutable = $this->commands[$command]; |
| 64 | return $commandExecutable->run($apieContext, $arguments); |
| 65 | } |
| 66 | } |