Completed
Push — master ( 984d76...8152ca )
by WEBEWEB
04:38
created

FTPClient   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 155
rs 10
c 0
b 0
f 0

10 Methods

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