Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
PassCommand
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
6.07
0.00% covered (danger)
0.00%
0 / 1
 run
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
6.07
1<?php
2namespace Apie\FtpServer\Commands;
3
4use Apie\Common\Exceptions\CanNotLoginException;
5use Apie\Common\LoginService;
6use Apie\Core\Context\ApieContext;
7use Apie\Core\ContextConstants;
8use Apie\FtpServer\FtpConstants;
9use React\Socket\ConnectionInterface;
10
11class PassCommand implements CommandInterface
12{
13    public function run(ApieContext $apieContext, string $arg = ''): ApieContext
14    {
15        $conn = $apieContext->getContext(ConnectionInterface::class);
16        $loginService = $apieContext->getContext(LoginService::class, false);
17        if ($loginService instanceof LoginService) {
18            $username = $apieContext->getContext(FtpConstants::USERNAME, false);
19            if ($username) {
20                try {
21                    $authenticated = $loginService->authorize($username, $arg, $apieContext);
22                    $conn->write("230 User logged in\r\n");
23                    return $apieContext->withContext(ContextConstants::AUTHENTICATED_USER, $authenticated);
24                } catch (CanNotLoginException) {
25                }
26            }
27            if ($username === 'anonymous' && $arg === '') {
28                $conn->write("230 User logged in\r\n");
29                return $apieContext;
30            }
31        } else {
32            $conn->write("230 User logged in\r\n");
33            return $apieContext;
34        }
35        $conn->write("430 Invalid username/password\r\n");
36        return $apieContext;
37    }
38}