CommandStream   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 21
c 0
b 0
f 0
dl 0
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 19 3
A __construct() 0 7 1
A open() 0 8 2
1
<?php
2
3
/**
4
 * This file is part of the Lazzard/ftp-bridge package.
5
 *
6
 * (c) El Amrani Chakir <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lazzard\FtpBridge\Stream;
13
14
use Lazzard\FtpBridge\Logger\LoggerInterface;
15
16
/**
17
 * Represents an FTP command stream (control channel).
18
 *
19
 * @since  1.0
20
 * @author El Amrani Chakir <[email protected]>
21
 * 
22
 * @internal
23
 */
24
class CommandStream extends Stream
25
{
26
    /** @var string */
27
    public $host;
28
29
    /** @var int */
30
    public $port;
31
32
    /** @var int */
33
    public $timeout;
34
35
    /** @var bool */
36
    public $blocking;
37
38
    /**
39
     * CommandStream constructor.
40
     *
41
     * @param LoggerInterface $logger
42
     * @param string          $host
43
     * @param int             $port
44
     * @param int             $timeout
45
     * @param bool            $blocking
46
     */
47
    public function __construct($logger, $host, $port, $timeout, $blocking)
48
    {
49
        parent::__construct($logger);
50
        $this->host     = $host;
51
        $this->port     = $port;
52
        $this->timeout  = $timeout;
53
        $this->blocking = $blocking;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function read()
60
    {
61
        $response = '';
62
        while (true) {
63
            $line      = fgets($this->stream);
64
            $response .= $line;
65
66
            // To distinguish the end of an FTP reply, the RFC959 indicates that the last line of
67
            // a the reply must be on a special format, it must be begin with 3 digits followed
68
            // by a space.
69
            //@link https://tools.ietf.org/html/rfc959#section-4
70
            if (preg_match('/^\d{3}+ /', $line) !== 0) {
71
                break;
72
            }
73
        }
74
75
        $this->log($response);
76
77
        return $response;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function open()
84
    {
85
        if($open = $this->openStreamSocket($this->host, $this->port, $this->timeout, $this->blocking)) {
86
            // TODO check the reply
87
            $this->read();
88
        }
89
90
        return $open;
91
    }
92
}