Completed
Push — release/1.0 ( 656514...db7b8d )
by Johnny
02:29
created

Ftp::write()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.2
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
namespace Redbox\Scan\Adapter;
3
use Symfony\Component\Yaml;
4
use Redbox\Scan\Report;
5
6
/**
7
 * Read and write files from a given ftp location.
8
 * see examples/ftp.php for a demonstration.
9
 *
10
 * @package Redbox\Scan\Adapter
11
 */
12
class Ftp implements AdapterInterface
13
{
14
    const FTP_MODE_ASCII  = FTP_ASCII;
15
    const FTP_MODE_BINARY = FTP_BINARY;
16
17
    protected $host = '';
18
    protected $username = '';
19
    protected $password = '';
20
    protected $filename = '';
21
    protected $port     = 21;
22
23
    protected $timeout  = 90;
24
    protected $handle   = null;
25
26
    /**
27
     * You might think just connect to the ftp server from the constructor
28
     * but psr-4 dictates that autoloadable classes MUST NOT...
29
     *
30
     * Quote:
31
     * Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value.
32
     *
33
     * So we need to use authenticate() after we construct.
34
     *
35
     * @param string $host
36
     * @param string $username
37
     * @param string $password
38
     * @param string $filename
39
     * @param int $port;
0 ignored issues
show
Bug introduced by
There is no parameter named $port;. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     * @param int $timeout
41
     */
42
    public function __construct($host = "", $username = "", $password = "", $filename = "", $port = 21, $timeout = 90)
43
    {
44
        $this->host     = $host;
45
        $this->username = $username;
46
        $this->password = $password;
47
        $this->filename = $filename;
48
        $this->timeout  = $timeout;
49
        $this->port     = $port;
50
    }
51
52
    /**
53
     * We should be so nice to terminate the construction of we are done.
54
     */
55
    public function __destruct() {
56
        if ($this->handle) {
57
            ftp_close($this->handle);
58
        }
59
    }
60
61
    public function authenticate() {
62
        $this->handle = ftp_connect($this->host, $this->port, $this->timeout);
63
        if (!$this->handle) // TODO: Reevaluate this
64
            return false;
65
66
        return ftp_login($this->handle, $this->username, $this->password);
67
    }
68
69
    /**
70
     * Read the previous scan results from the file system.
71
     *
72
     * @return array
73
     */
74
    public function read() {
75
        $stream = fopen('php://memory', 'w');
76
        $data   = '';
77
        if (!$stream) return false;
78
        if ($ret = ftp_nb_fget($this->handle, $stream, $this->filename, self::FTP_MODE_ASCII)) {
79
            while ($ret === FTP_MOREDATA) {
80
                rewind($stream);
81
                $data .=  stream_get_contents($stream);
82
                $ret = ftp_nb_continue($this->handle);
83
            }
84
            if ($ret != FTP_FINISHED) {
85
               return false;
86
            } else {
87
                $data = Yaml\Yaml::parse($data);
88
                return $data;
89
            }
90
        }
91
        return false;
92
    }
93
94
    // TODO: This should be an universial exception
95
    /**
96
     * Write the report to the filesystem so we can reuse it
97
     * at a later stace when we invoke Redbox\Scan\ScanService's scan() method.
98
     *
99
     * @param Report\Report|null $report
100
     * @return bool
101
     */
102
    public function write(Report\Report $report = null) {
103
        if ($report) {
104
            $stream = fopen('php://memory', 'w+');
105
            if (!$stream) return false;
106
            $data = $report->toArray();
107
            $data = Yaml\Yaml::dump($data, 99);
108
109
            fwrite($stream, $data);
110
            rewind($stream);
111
       //     fclose($stream);
112
113
            if(ftp_fput($this->handle, $this->filename, $stream, self::FTP_MODE_ASCII)) {
114
                return true;
115
            } else {
116
                return false;
117
            }
118
        }
119
        return false;
120
    }
121
122
}