Completed
Push — develop ( f5e8cd...b17c3e )
by Novikov
01:21
created

SFTP   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 18.68 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 9 2
A connectWithKey() 0 9 1
A getRemoteFilesList() 0 11 2
A disconnect() 0 5 1
A fetchFrom() 9 9 2
A sendTo() 8 8 2

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
    /**
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 fetchFrom($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 sendTo($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