Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
21 / 27
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FtpServerRunner
77.78% covered (warning)
77.78%
21 / 27
66.67% covered (warning)
66.67%
2 / 3
4.18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
1
 run
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\FtpServer;
3
4use Apie\Core\Context\ApieContext;
5use Apie\FtpServer\Commands\CdupCommand;
6use Apie\FtpServer\Commands\CwdCommand;
7use Apie\FtpServer\Commands\EprtCommand;
8use Apie\FtpServer\Commands\EpsvCommand;
9use Apie\FtpServer\Commands\ListCommand;
10use Apie\FtpServer\Commands\NlstCommand;
11use Apie\FtpServer\Commands\PassCommand;
12use Apie\FtpServer\Commands\PasvCommand;
13use Apie\FtpServer\Commands\PortCommand;
14use Apie\FtpServer\Commands\PwdCommand;
15use Apie\FtpServer\Commands\QuitCommand;
16use Apie\FtpServer\Commands\RetrCommand;
17use Apie\FtpServer\Commands\SiteCommand;
18use Apie\FtpServer\Commands\SystCommand;
19use Apie\FtpServer\Commands\TypeCommand;
20use Apie\FtpServer\Commands\UserCommand;
21use Apie\FtpServer\Lists\CommandHashmap;
22use Apie\FtpServer\SiteCommands\SiteCommandInterface;
23use React\Socket\ConnectionInterface;
24
25class 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}