Completed
Push — develop ( 70f03a...98593a )
by Novikov
07:02
created

SFTP::getRemoteFilesList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace SF2Helpers\SFTPBundle\SFTP;
4
5
class SFTP implements ConnectionInterface, ResourceTransferInterface
6
{
7
    /**
8
     * Connection instance
9
     *
10
     * @var $connection
11
     */
12
    private $connection;
13
14
    /**
15
     * Established sftp resource
16
     *
17
     * @var $sftp
18
     */
19
    private $sftp;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function connect($host, $username, $password = null)
25
    {
26
        $connection = ssh2_connect($host);
27
28
        is_null($password) ? ssh2_auth_agent($connection, $username) : ssh2_auth_password($connection, $username, $password);
29
30
        $this->connection = $connection;
31
        $this->sftp       = ssh2_sftp($connection);
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function connectWithKey($host, $username, $pubkeyfile, $privkeyfile, $passphrase = null)
38
    {
39
        $connection = ssh2_connect($host);
40
41
        ssh2_auth_pubkey_file($connection, $username, $pubkeyfile, $privkeyfile, $passphrase);
42
43
        $this->connection = $connection;
44
        $this->sftp       = ssh2_sftp($connection);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 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...
51
    {
52
        $sftp = "ssh2.sftp://$this->sftp";
53
        $data = file_get_contents($sftp . $remoteFile);
54
        if (!$data) {
55
            throw new \Exception('File can`t be loaded from server');
56
        }
57
        file_put_contents($localFile, $data);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 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...
64
    {
65
        $sftp = "ssh2.sftp://$this->sftp";
66
        $data = file_get_contents($localFile);
67
        if (!file_put_contents($sftp . $remoteFile, $data)) {
68
            throw new \Exception('File could not be uploaded to server');
69
        }
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function getRemoteFilesList($dir)
76
    {
77
        $handle = opendir("ssh2.sftp://$this->sftp" . $dir);
78
        $files  = [];
79
80
        while (false !== ($entry = readdir($handle))) {
81
            $files[] = $entry;
82
        }
83
84
        return $files;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function disconnect()
91
    {
92
        ssh2_exec($this->connection, 'exit');
93
        unset($this->connection);
94
    }
95
}
96