Completed
Push — master ( baf6b1...a46b20 )
by WEBEWEB
01:16
created

FtpClient::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Library\Core\Network\FTP\Client;
13
14
use WBW\Library\Core\Network\FTP\Exception\FtpException;
15
use WBW\Library\Core\Security\Authenticator;
16
17
/**
18
 * FTP client.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Network\FTP\Client
22
 */
23
class FtpClient extends AbstractFtpClient {
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param Authenticator $authenticator The authenticator.
29
     */
30
    public function __construct(Authenticator $authenticator) {
31
        parent::__construct($authenticator);
32
33
        $this->getAuthenticator()->setScheme("ftp");
34
        if (null === $this->getAuthenticator()->getPort()) {
35
            $this->getAuthenticator()->setPort(21);
36
        }
37
    }
38
39
    /**
40
     * Close.
41
     *
42
     * @return FtpClient Returns this FTP client.
43
     * @throws FtpException Throws a FTP exception if an error occurs.
44
     */
45
    public function close() {
46
47
        if (false === @ftp_close($this->getConnection())) {
48
            throw $this->newFtpException("ftp_close failed");
49
        }
50
51
        return $this;
52
    }
53
54
    /**
55
     * Connect.
56
     *
57
     * @param int $timeout The timeout.
58
     * @return FtpClient Returns this FTP client.
59
     * @throws FtpException Throws a FTP exception if an error occurs.
60
     */
61
    public function connect($timeout = 90) {
62
63
        $host = $this->getAuthenticator()->getHostname();
64
        $port = $this->getAuthenticator()->getPort();
65
66
        $this->setConnection(@ftp_connect($host, $port, $timeout));
67
        if (false === $this->getConnection()) {
68
            throw $this->newFtpException("ftp_connect failed: [${host}, ${port}]");
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * Delete.
76
     *
77
     * @param string $path The path.
78
     * @return FtpClient Returns this FTP client.
79
     * @throws FtpException Throws a FTP exception if an error occurs.
80
     */
81
    public function delete($path) {
82
83
        if (false === @ftp_delete($this->getConnection(), $path)) {
84
            throw $this->newFtpException("ftp_delete failed: [${path}]");
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * Get a file.
92
     *
93
     * @param string $localFile The local file.
94
     * @param string $remoteFile The remote file.
95
     * @return FtpClient Returns this FTP client.
96
     * @throws FtpException Throws a FTP exception if an error occurs.
97
     */
98
    public function get($localFile, $remoteFile) {
99
100
        if (false === @ftp_get($this->getConnection(), $localFile, $remoteFile)) {
101
            throw $this->newFtpException("ftp_get failed: [${localFile}, ${remoteFile}]");
102
        }
103
104
        return $this;
105
    }
106
107
    /**
108
     * Login.
109
     *
110
     * @return FtpClient Returns this FTP client.
111
     * @throws FtpException Throws a FTP exception if an error occurs.
112
     */
113
    public function login() {
114
115
        $username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername();
116
        $password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword();
117
118
        if (false === @ftp_login($this->getConnection(), $username, $password)) {
119
            throw $this->newFtpException("ftp_login failed: [${username}]");
120
        }
121
122
        return $this;
123
    }
124
125
    /**
126
     * Make a directory.
127
     *
128
     * @param string $directory The directory.
129
     * @return FtpClient Returns this FTP client.
130
     * @throws FtpException Throws a FTP exception if an error occurs.
131
     */
132
    public function mkdir($directory) {
133
134
        if (false === @ftp_mkdir($this->getConnection(), $directory)) {
135
            throw $this->newFtpException("mkdir failed: [${directory}]");
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Set the passive mode.
143
     *
144
     * @param bool $pasv The passive mode.
145
     * @return FtpClient Returns this FTP client.
146
     * @throws FtpException Throws a FTP exception if an error occurs.
147
     */
148
    public function pasv($pasv) {
149
150
        if (false === @ftp_pasv($this->getConnection(), $pasv)) {
151
            throw $this->newFtpException("ftp_pasv failed: [${pasv}]");
152
        }
153
154
        return $this;
155
    }
156
157
    /**
158
     * Put a file.
159
     *
160
     * @param string $localFile The local file.
161
     * @param string $remoteFile The remote file.
162
     * @param int $mode The mode.
163
     * @param int $startPos The start position.
164
     * @return FtpClient Returns this FTP client.
165
     * @throws FtpException Throws a FTP exception if an error occurs.
166
     */
167
    public function put($localFile, $remoteFile, $mode = FTP_IMAGE, $startPos = 0) {
168
169
        if (false === @ftp_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos)) {
170
            throw $this->newFtpException("ftp_put failed: [${remoteFile}, ${localFile}]");
171
        }
172
173
        return $this;
174
    }
175
176
    /**
177
     * Rename.
178
     *
179
     * @param string $oldName The old name.
180
     * @param string $newName The new name.
181
     * @return FtpClient Returns this FTP client.
182
     * @throws FtpException Throws a FTP exception if an error occurs.
183
     */
184
    public function rename($oldName, $newName) {
185
186
        if (false === @ftp_rename($this->getConnection(), $oldName, $newName)) {
187
            throw $this->newFtpException("ftp_rename failed: [${oldName}, ${newName}]");
188
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Remove a directory.
195
     *
196
     * @param string $directory The directory.
197
     * @return FtpClient Returns this FTP client.
198
     * @throws FtpException Throws a FTP exception if an error occurs.
199
     */
200
    public function rmdir($directory) {
201
202
        if (false === @ftp_rmdir($this->getConnection(), $directory)) {
203
            throw $this->newFtpException("ftp_rmdir failed: [${directory}]");
204
        }
205
206
        return $this;
207
    }
208
}
209