Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
NlstCommand
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
3.00
0.00% covered (danger)
0.00%
0 / 1
 run
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
3.00
1<?php
2namespace Apie\FtpServer\Commands;
3
4use Apie\ApieFileSystem\ApieFilesystem;
5use Apie\ApieFileSystem\Virtual\VirtualFileInterface;
6use Apie\ApieFileSystem\Virtual\VirtualFolderInterface;
7use Apie\Core\Context\ApieContext;
8use Apie\FtpServer\FtpConstants;
9use Apie\FtpServer\Transfers\TransferInterface;
10use React\Socket\ConnectionInterface;
11
12class NlstCommand implements CommandInterface
13{
14    public function run(ApieContext $apieContext, string $arg = ''): ApieContext
15    {
16        $conn = $apieContext->getContext(ConnectionInterface::class);
17        $filesystem = $apieContext->getContext(ApieFilesystem::class);
18        assert($filesystem instanceof ApieFilesystem);
19        $conn->write("150 Here comes the directory listing.\r\n");
20        $transfer = $apieContext->getContext(TransferInterface::class);
21        $currentFolder = $arg ? $filesystem->visit($arg) : $apieContext->getContext(FtpConstants::CURRENT_FOLDER);
22        if ($currentFolder instanceof VirtualFolderInterface) {
23            $files = array_map(
24                function (VirtualFolderInterface|VirtualFileInterface $child) {
25                    return $child->getName();
26                },
27                $currentFolder->getChildren()->toArray()
28            );
29            $transfer->send(implode("\r\n", $files) . "\r\n");
30            $transfer->end();
31            $conn->write("226 NLST command successful.\r\n");
32        } else {
33            $conn->write("550 Path is not a folder.\r\n");
34        }
35        return $apieContext;
36    }
37}