Completed
Pull Request — develop (#1)
by
unknown
03:59
created

SFTP::connectWithKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 5
1
<?php
2
3
namespace SF2Helpers\SFTPBundle\SFTP;
4
5
class SFTP implements ConnectionInterface, ResourceTransferInterface
6
{
7
    private $connection; // connection instance
8
9
    private $sftp; // established sftp
10
11
12
    public function __construct()
13
    {
14
    }
15
16
    public function connect($host, $username, $password=null)
17
    {
18
        $connection = ssh2_connect($host);
19
20
        if (is_null($password)) {
21
            ssh2_auth_agent($connection, $username);
22
        } else {
23
            ssh2_auth_password($connection, $username, $password);
24
        }
25
26
        $sftp = ssh2_sftp($connection);
27
        $this->connection = $connection;
28
        $this->sftp = $sftp;
29
    }
30
31
    public function connectWithKey($host, $username, string $pubkeyfile, string $privkeyfile, string $passphrase=NULL)
32
    {
33
        $connection = ssh2_connect($host);
34
35
        ssh2_auth_pubkey_file($connection, $username, $pubkeyfile, $privkeyfile, $passphrase);
36
37
        $sftp = ssh2_sftp($connection);
38
        $this->connection = $connection;
39
        $this->sftp = $sftp;
40
    }
41
42 View Code Duplication
    public function copy($remoteFile, $localFile)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $sftp = "ssh2.sftp://$this->sftp";
45
        $data = file_get_contents($sftp . $remoteFile);
46
        if (!$data) {
47
            throw new \Exception('File can`t be loaded from server');
48
        }
49
        file_put_contents($localFile, $data);
50
    }
51
52 View Code Duplication
    public function send($localFile, $remoteFile)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $sftp = "ssh2.sftp://$this->sftp";
55
        $data = file_get_contents($localFile);
56
        if (!file_put_contents($sftp . $remoteFile, $data)) {
57
            throw new \Exception('File could not be uploaded to server');
58
        }
59
    }
60
61
    public function getFilesList($dir) {
62
        $handle = opendir("ssh2.sftp://$this->sftp" . $dir);
63
        $files = array();
64
65
        while (false != ($entry = readdir($handle))){
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $entry = readdir($handle) of type string to the boolean false. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
66
            $files[] = $entry;
67
        }
68
69
        return $files;
70
    }
71
72
    public function disconnect()
73
    {
74
        ssh2_exec($this->connection, 'exit');
75
        unset($this->connection);
76
    }
77
}
78