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