Completed
Push — master ( 5688ec...106f15 )
by WEBEWEB
01:33
created

FTPClient::rmdir()   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
 * FTP client.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\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
        $this->getAuthenticator()->setScheme("ftp");
33
        if (null === $this->getAuthenticator()->getPort()) {
34
            $this->getAuthenticator()->setPort(21);
35
        }
36
    }
37
38
    /**
39
     * Closes this FTP connection.
40
     *
41
     * @return FTPClient Returns this FTP client.
42
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
43
     */
44
    public function close() {
45
        if (false === @ftp_close($this->getConnection())) {
46
            throw $this->newFTPException("close failed");
47
        }
48
        return $this;
49
    }
50
51
    /**
52
     * Opens this FTP connection.
53
     *
54
     * @param integer $timeout The timeout.
55
     * @return FTPClient Returns this FTP client.
56
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
57
     */
58
    public function connect($timeout = 90) {
59
        $host = $this->getAuthenticator()->getHost();
60
        $port = $this->getAuthenticator()->getPort();
61
        $this->setConnection(@ftp_connect($host, $port, $timeout));
62
        if (false === $this->getConnection()) {
63
            throw $this->newFTPException("connection failed");
64
        }
65
        return $this;
66
    }
67
68
    /**
69
     * Deletes a file on the FTP server.
70
     *
71
     * @param string $path The file to delete.
72
     * @return FTPClient Returns this FTP client.
73
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
74
     */
75
    public function delete($path) {
76
        if (false === @ftp_delete($this->getConnection(), $path)) {
77
            throw $this->newFTPException(sprintf("delete %s failed", $path));
78
        }
79
        return $this;
80
    }
81
82
    /**
83
     * Logs in to this FTP connection.
84
     *
85
     * @return FTPClient Returns this FTP client.
86
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
87
     */
88
    public function login() {
89
        $username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername();
90
        $password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword();
91
        if (false === @ftp_login($this->getConnection(), $username, $password)) {
92
            throw $this->newFTPException("login failed");
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * Tuns passive mode on or off.
99
     *
100
     * @param boolean $pasv The passive mode.
101
     * @return FTPClient Returns this FTP client.
102
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
103
     */
104
    public function pasv($pasv) {
105
        if (false === @ftp_pasv($this->getConnection(), $pasv)) {
106
            throw $this->newFTPException(sprintf("pasv from %d to %d failed", !$pasv, $pasv));
107
        }
108
        return $this;
109
    }
110
111
    /**
112
     * Uploads a file to The FTP server.
113
     *
114
     * @param string $localFile The local file.
115
     * @param string $remoteFile The remote file.
116
     * @param integer $mode The mode.
117
     * @param integer $startPos The start position.
118
     * @return FTPClient Returns this FTP client.
119
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
120
     */
121
    public function put($localFile, $remoteFile, $mode = FTP_IMAGE, $startPos = 0) {
122
        if (false === @ftp_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos)) {
123
            throw $this->newFTPException(sprintf("put %s into %s failed", $localFile, $remoteFile));
124
        }
125
        return $this;
126
    }
127
128
    /**
129
     * Creates a directory.
130
     *
131
     * @param string $directory The directory.
132
     * @return FTPClient Returns this FTP client.
133
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
134
     */
135
    public function mkdir($directory) {
136
        if (false === @ftp_mkdir($this->getConnection(), $directory)) {
137
            throw $this->newFTPException(sprintf("mkdir %s failed", $directory));
138
        }
139
        return $this;
140
    }
141
142
    /**
143
     * Renames a file or a directory on the FTP server.
144
     *
145
     * @param string $oldName The old file/directory name.
146
     * @param string $newName The new name.
147
     * @return FTPClient Returns this FTP client.
148
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
149
     */
150
    public function rename($oldName, $newName) {
151
        if (false === @ftp_rename($this->getConnection(), $oldName, $newName)) {
152
            throw $this->newFTPException(sprintf("rename %s into %s failed", $oldName, $newName));
153
        }
154
        return $this;
155
    }
156
157
    /**
158
     * Removes a directory.
159
     *
160
     * @param string $directory The directory.
161
     * @return FTPClient Returns this FTP client.
162
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
163
     */
164
    public function rmdir($directory) {
165
        if (false === @ftp_rmdir($this->getConnection(), $directory)) {
166
            throw $this->newFTPException(sprintf("rmdir %s failed", $directory));
167
        }
168
        return $this;
169
    }
170
171
}
172