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

SFTP   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 23.29 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 17
loc 73
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A connect() 0 14 2
A connectWithKey() 0 10 1
A copy() 9 9 2
A send() 8 8 2
A getFilesList() 0 10 2
A disconnect() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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