Completed
Push — master ( c34a5d...56c72e )
by WEBEWEB
01:18
created

AbstractFTPClient::mkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the ftp-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\FTP\Client;
13
14
use WBW\Library\Core\Security\Authenticator;
15
use WBW\Library\FTP\Exception\FTPException;
16
17
/**
18
 * Abstract FTP client.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\FTP\Client
22
 * @abstract
23
 */
24
abstract class AbstractFTPClient {
25
26
    /**
27
     * Authenticator.
28
     *
29
     * @var Authenticator
30
     */
31
    private $authenticator;
32
33
    /**
34
     * Connection.
35
     *
36
     * @var mixed
37
     */
38
    private $connection;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param Authenticator $authenticator The authenticator.
44
     */
45
    protected function __construct(Authenticator $authenticator) {
46
        $this->authenticator = $authenticator;
47
    }
48
49
    /**
50
     * Closes this FTP connection.
51
     *
52
     * @return AbstractFTPClient Returns this abstract FTP client.
53
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
54
     */
55
    public function close() {
56
        if (false === @ftp_close($this->getConnection())) {
57
            throw $this->newFTPException("close failed");
58
        }
59
        return $this;
60
    }
61
62
    /**
63
     * Opens this FTP connection.
64
     *
65
     * @param integer $timeout The timeout.
66
     * @return AbstractFTPClient Returns this abstract FTP client.
67
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
68
     */
69
    abstract public function connect($timeout = 90);
70
71
    /**
72
     * Deletes a file on the FTP server.
73
     *
74
     * @param string $path The file to delete.
75
     * @return AbstractFTPClient Returns this abstract FTP client.
76
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
77
     */
78
    public function delete($path) {
79
        if (false === @ftp_delete($this->getConnection(), $path)) {
80
            throw $this->newFTPException(sprintf("delete %s failed", $path));
81
        }
82
        return $this;
83
    }
84
85
    /**
86
     * Get the authenticator.
87
     *
88
     * @return Authenticator Returns the authenticator.
89
     */
90
    final public function getAuthenticator() {
91
        return $this->authenticator;
92
    }
93
94
    /**
95
     * Get the connection.
96
     *
97
     * @return mixed Returns the connection.
98
     */
99
    final public function getConnection() {
100
        return $this->connection;
101
    }
102
103
    /**
104
     * Logs in to this FTP connection.
105
     *
106
     * @return AbstractFTPClient Returns this abstract FTP client.
107
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
108
     */
109
    public function login() {
110
        $username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername();
111
        $password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword();
112
        if (false === @ftp_login($this->getConnection(), $username, $password)) {
113
            throw $this->newFTPException("login failed");
114
        }
115
        return $this;
116
    }
117
118
    /**
119
     * Construct a new FTP exception.
120
     *
121
     * @param string $message The message.
122
     * @return FTPException Returns a new FTP exception.
123
     */
124
    final protected function newFTPException($message) {
125
        return new FTPException(sprintf("%s://%s:%s@%s:%d " . $message, $this->authenticator->getScheme(), $this->authenticator->getPasswordAuthentication()->getUsername(), $this->authenticator->getPasswordAuthentication()->getPassword(), $this->authenticator->getHost(), $this->authenticator->getPort()));
0 ignored issues
show
Bug introduced by
The method getScheme() does not seem to exist on object<WBW\Library\Core\Security\Authenticator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
    }
127
128
    /**
129
     * Tuns passive mode on or off.
130
     *
131
     * @param boolean $pasv The passive mode.
132
     * @return AbstractFTPClient Returns this abstract FTP client.
133
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
134
     */
135
    public function pasv($pasv) {
136
        if (false === @ftp_pasv($this->getConnection(), $pasv)) {
137
            throw $this->newFTPException(sprintf("pasv from %d to %d failed", !$pasv, $pasv));
138
        }
139
        return $this;
140
    }
141
142
    /**
143
     * Uploads a file to The FTP server.
144
     *
145
     * @param string $localFile The local file.
146
     * @param string $remoteFile The remote file.
147
     * @param integer $mode The mode.
148
     * @param integer $startPos The start position.
149
     * @return AbstractFTPClient Returns this abstract FTP client.
150
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
151
     */
152
    public function put($localFile, $remoteFile, $mode = FTP_IMAGE, $startPos = 0) {
153
        if (false === @ftp_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos)) {
154
            throw $this->newFTPException(sprintf("put %s into %s failed", $localFile, $remoteFile));
155
        }
156
        return $this;
157
    }
158
159
    /**
160
     * Creates a directory.
161
     *
162
     * @param string $directory The directory.
163
     * @return AbstractFTPClient Returns this abstract FTP client.
164
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
165
     */
166
    public function mkdir($directory) {
167
        if (false === @ftp_mkdir($this->getConnection(), $directory)) {
168
            throw $this->newFTPException(sprintf("mkdir %s failed", $directory));
169
        }
170
        return $this;
171
    }
172
173
    /**
174
     * Renames a file or a directory on the FTP server.
175
     *
176
     * @param string $oldName The old file/directory name.
177
     * @param string $newName The new name.
178
     * @return AbstractFTPClient Returns this abstract FTP client.
179
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
180
     */
181
    public function rename($oldName, $newName) {
182
        if (false === @ftp_rename($this->getConnection(), $oldName, $newName)) {
183
            throw $this->newFTPException(sprintf("rename %s into %s failed", $oldName, $newName));
184
        }
185
        return $this;
186
    }
187
188
    /**
189
     * Removes a directory.
190
     *
191
     * @param string $directory The directory.
192
     * @return AbstractFTPClient Returns this abstract FTP client.
193
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
194
     */
195
    public function rmdir($directory) {
196
        if (false === @ftp_rmdir($this->getConnection(), $directory)) {
197
            throw $this->newFTPException(sprintf("rmdir %s failed", $directory));
198
        }
199
        return $this;
200
    }
201
202
    /**
203
     * Set the authenticator.
204
     *
205
     * @param \WBW\Library\FTP\Client\Authenticator $authenticator The authenticator.
206
     * @returns AbstractFTPClient Returns this abstract FTP client.
207
     */
208
    final protected function setAuthenticator(Authenticator $authenticator) {
209
        $this->authenticator = $authenticator;
210
        return $this;
211
    }
212
213
    /**
214
     * Set the connection.
215
     *
216
     * @param mixed $connection The connection.
217
     * @returns AbstractFTPClient Returns this abstract FTP client.
218
     */
219
    final protected function setConnection($connection) {
220
        $this->connection = $connection;
221
        return $this;
222
    }
223
224
}
225