GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#1061)
by Maxim
04:23 queued 01:35
created

PhpSecLib   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 143
Duplicated Lines 16.08 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 4.34%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 143
ccs 3
cts 69
cp 0.0434
rs 10
wmc 19
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C connect() 17 46 7
A checkConnection() 0 6 2
A run() 0 13 3
A upload() 3 16 3
A download() 3 8 2
A getConfiguration() 0 4 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
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Server\Remote;
9
10
use Deployer\Server\Configuration;
11
use Deployer\Server\ServerInterface;
12
use phpseclib\Crypt\RSA;
13
use phpseclib\Net\SFTP;
14
use phpseclib\System\SSH\Agent;
15
use RuntimeException;
16
17
class PhpSecLib implements ServerInterface
18
{
19
    /**
20
     * @var Configuration
21
     */
22
    private $configuration;
23
24
    /**
25
     * @var SFTP
26
     */
27
    private $sftp;
28
29
    /**
30
     * Array of created directories during upload.
31
     * @var array
32
     */
33
    private $directories = [];
34
35
    /**
36
     * @param Configuration $configuration
37
     */
38 5
    public function __construct(Configuration $configuration)
39
    {
40 5
        $this->configuration = $configuration;
41 5
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function connect()
47
    {
48
        $serverConfig = $this->getConfiguration();
49
        $this->sftp = new SFTP($serverConfig->getHost(), $serverConfig->getPort(), 3600);
50
51
        switch ($serverConfig->getAuthenticationMethod()) {
52
            case Configuration::AUTH_BY_PASSWORD:
53
                $authParams = [$serverConfig->getUser(), $serverConfig->getPassword()];
54
                break;
55
56 View Code Duplication
            case Configuration::AUTH_BY_IDENTITY_FILE:
57
                $key = new RSA();
58
                $key->setPassword($serverConfig->getPassPhrase());
59
                $key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
60
                $authParams = [$serverConfig->getUser(), $key];
61
                break;
62
63 View Code Duplication
            case Configuration::AUTH_BY_PEM_FILE:
64
                $key = new RSA();
65
                $key->loadKey(file_get_contents($serverConfig->getPemFile()));
66
                $authParams = [$serverConfig->getUser(), $key];
67
                break;
68
69
            case Configuration::AUTH_BY_AGENT:
70
                $key = new Agent();
71
                $key->startSSHForwarding(null);
72
                $authParams = [$serverConfig->getUser(), $key];
73
                break;
74
75 View Code Duplication
            case Configuration::AUTH_BY_IDENTITY_FILE_AND_PASSWORD:
76
                $key = new RSA();
77
                $key->setPassword($serverConfig->getPassPhrase());
78
                $key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
79
                $authParams = [$serverConfig->getUser(), $key, $serverConfig->getPassword()];
80
                break;
81
82
            default:
83
                throw new RuntimeException('You need to specify authentication method.');
84
        }
85
86
        $result = call_user_func_array([$this->sftp, 'login'], $authParams);
87
88
        if (!$result) {
89
            throw new RuntimeException('Unable to login with the provided credentials.');
90
        }
91
    }
92
93
    /**
94
     * Check if not connected and connect.
95
     */
96
    public function checkConnection()
97
    {
98
        if (null === $this->sftp) {
99
            $this->connect();
100
        }
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function run($command)
107
    {
108
        $this->checkConnection();
109
110
        $result = $this->sftp->exec($command);
111
112
        if ($this->sftp->getExitStatus() !== 0) {
113
            $output = $this->sftp->getStdError() ?: $result;
114
            throw new \RuntimeException($output);
115
        }
116
117
        return $result;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function upload($local, $remote)
124
    {
125
        $this->checkConnection();
126
127
        $remote = str_replace(DIRECTORY_SEPARATOR, '/', $remote);
128
        $dir = str_replace(DIRECTORY_SEPARATOR, '/', dirname($remote));
129
130
        if (!isset($this->directories[$dir])) {
131
            $this->sftp->mkdir($dir, -1, true);
132
            $this->directories[$dir] = true;
133
        }
134
135 View Code Duplication
        if (!$this->sftp->put($remote, $local, SFTP::SOURCE_LOCAL_FILE)) {
136
            throw new \RuntimeException(implode(PHP_EOL, $this->sftp->getSFTPErrors()));
137
        }
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function download($local, $remote)
144
    {
145
        $this->checkConnection();
146
147 View Code Duplication
        if (!$this->sftp->get($remote, $local)) {
148
            throw new \RuntimeException(implode(PHP_EOL, $this->sftp->getSFTPErrors()));
149
        }
150
    }
151
152
    /**
153
     * @return Configuration
154
     */
155
    public function getConfiguration()
156
    {
157
        return $this->configuration;
158
    }
159
}
160