Completed
Push — develop ( f78c15...62bc88 )
by Novikov
02:01
created

SFTP::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
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
     * @param string $host
13
     * @param string $username
14
     * @param string|null $password
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
    /**
32
     * @param string $host
33
     * @param string $username
34
     * @param string  $pubkeyfile
35
     * @param string $privkeyfile
36
     * @param string|null $passphrase
37
     */
38
    public function connectWithKey($host, $username, $pubkeyfile, $privkeyfile, $passphrase = null)
39
    {
40
        $connection = ssh2_connect($host);
41
42
        ssh2_auth_pubkey_file($connection, $username, $pubkeyfile, $privkeyfile, $passphrase);
43
44
        $sftp = ssh2_sftp($connection);
45
        $this->connection = $connection;
46
        $this->sftp = $sftp;
47
    }
48
49
    /**
50
     * @param $remoteFile
51
     * @param $localFile
52
     * @throws \Exception
53
     */
54 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...
55
    {
56
        $sftp = "ssh2.sftp://$this->sftp";
57
        $data = file_get_contents($sftp . $remoteFile);
58
        if (!$data) {
59
            throw new \Exception('File can`t be loaded from server');
60
        }
61
        file_put_contents($localFile, $data);
62
    }
63
64
    /**
65
     * @param $localFile
66
     * @param $remoteFile
67
     * @throws \Exception
68
     */
69 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...
70
    {
71
        $sftp = "ssh2.sftp://$this->sftp";
72
        $data = file_get_contents($localFile);
73
        if (!file_put_contents($sftp . $remoteFile, $data)) {
74
            throw new \Exception('File could not be uploaded to server');
75
        }
76
    }
77
78
    /**
79
     * @param $dir
80
     * @return array
81
     */
82
    public function getFilesList($dir)
83
    {
84
        $handle = opendir("ssh2.sftp://$this->sftp" . $dir);
85
        $files = array();
86
87
        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...
88
            $files[] = $entry;
89
        }
90
91
        return $files;
92
    }
93
94
    public function disconnect()
95
    {
96
        ssh2_exec($this->connection, 'exit');
97
        unset($this->connection);
98
    }
99
}
100