Completed
Push — master ( 106f15...afa6c9 )
by WEBEWEB
01:27
created

SFTPClient::mkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
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
 * SFTP client.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\FTP\Client
22
 */
23
class SFTPClient extends AbstractFTPClient {
24
25
    /**
26
     * SFTP resource.
27
     *
28
     * @var mixed
29
     */
30
    private $sftp;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param Authenticator $authenticator The authenticator.
36
     */
37
    public function __construct(Authenticator $authenticator) {
38
        parent::__construct($authenticator);
39
        $this->getAuthenticator()->setScheme("sftp");
40
        if (null === $this->getAuthenticator()->getPort()) {
41
            $this->getAuthenticator()->setPort(22);
42
        }
43
    }
44
45
    /**
46
     * Closes this SFTP connection.
47
     *
48
     * @return SFTPClient Returns this SFTP client.
49
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
50
     */
51
    public function close() {
52
        if (false === ssh2_exec($this->getConnection(), "exit")) {
53
            throw $this->newFTPException("close failed");
54
        }
55
        return $this;
56
    }
57
58
    /**
59
     * Opens this SFTP connection.
60
     *
61
     * @return SFTPClient Returns this SFTP client.
62
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
63
     */
64
    public function connect() {
65
        $host = $this->getAuthenticator()->getHost();
66
        $port = $this->getAuthenticator()->getPort();
67
        $this->setConnection(ssh2_connect($host, $port));
68
        if (false === $this->getConnection()) {
69
            throw $this->newFTPException("connection failed");
70
        }
71
        return $this;
72
    }
73
74
    /**
75
     * Deletes a file on the SFTP server.
76
     *
77
     * @param string $path The file to delete.
78
     * @return SFTPClient Returns this SFTP client.
79
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
80
     */
81
    public function delete($path) {
82
        if (false === ssh2_sftp_unlink($this->getSFTP(), $path)) {
83
            throw $this->newFTPException(sprintf("delete %s failed", $path));
84
        }
85
        return $this;
86
    }
87
88
    /**
89
     * Get the SFTP resource.
90
     *
91
     * @return mixed Returns the SFTP resource.
92
     */
93
    private function getSFTP() {
94
        if (null === $this->sftp) {
95
            $this->sftp = ssh2_sftp($this->getConnection());
96
        }
97
        return $this->sftp;
98
    }
99
100
    /**
101
     * Logs in to this SFTP connection.
102
     *
103
     * @return SFTPClient Returns this SFTP client.
104
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
105
     */
106
    public function login() {
107
        $username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername();
108
        $password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword();
109
        if (false === ssh2_auth_password($this->getConnection(), $username, $password)) {
110
            throw $this->newFTPException("login failed");
111
        }
112
        return $this;
113
    }
114
115
    /**
116
     * Creates a directory.
117
     *
118
     * @param string $directory The directory.
119
     * @param integer $mode The mode.
120
     * @param boolean $recursive Recursively ?.
121
     * @return SFTPClient Returns this SFTP client.
122
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
123
     */
124
    public function mkdir($directory, $mode = 0777, $recursive = false) {
125
        if (false === ssh2_sftp_mkdir($this->getSFTP(), $directory, $mode, $recursive)) {
126
            throw $this->newFTPException(sprintf("mkdir %s failed", $directory));
127
        }
128
        return $this;
129
    }
130
131
    /**
132
     * Uploads a file to the SFTP server.
133
     *
134
     * @param string $localFile The local file.
135
     * @param string $remoteFile The remote file.
136
     * @param integer $createMode The create mode.
0 ignored issues
show
Bug introduced by
There is no parameter named $createMode. 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...
137
     * @return SFTPClient Returns this SFTP client.
138
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
139
     */
140
    public function put($localFile, $remoteFile) {
141
        $stream = fopen("ssh2.sftp://" . intval($this->getSFTP()) . $remoteFile, "w");
142
        fwrite($stream, file_get_contents($localFile));
143
        fclose($stream);
144
        return $this;
145
    }
146
147
    /**
148
     * Renames a file or a directory on the SFTP server.
149
     *
150
     * @param string $oldName The old file/directory name.
151
     * @param string $newName The new name.
152
     * @return SFTPClient Returns this SFTP client.
153
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
154
     */
155
    public function rename($oldName, $newName) {
156
        if (false === ssh2_sftp_rename($this->getSFTP(), $oldName, $newName)) {
157
            throw $this->newFTPException(sprintf("rename %s into %s failed", $oldName, $newName));
158
        }
159
        return $this;
160
    }
161
162
    /**
163
     * Removes a directory.
164
     *
165
     * @param string $directory The directory.
166
     * @return SFTPClient Returns this SFTP client.
167
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
168
     */
169
    public function rmdir($directory) {
170
        if (false === ssh2_sftp_rmdir($this->getSFTP(), $directory)) {
171
            throw $this->newFTPException(sprintf("rmdir %s failed", $directory));
172
        }
173
        return $this;
174
    }
175
176
}
177