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.
Completed
Push — master ( 99078a...9605c1 )
by Oanh
02:49
created

PhpSecLib::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 2
    public function __construct(Configuration $configuration)
39
    {
40 2
        $this->configuration = $configuration;
41 2
    }
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:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
54
                $result = $this->sftp->login($serverConfig->getUser(), $serverConfig->getPassword());
55
56
                break;
57
58 View Code Duplication
            case Configuration::AUTH_BY_IDENTITY_FILE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
59
60
                $key = new RSA();
61
                $key->setPassword($serverConfig->getPassPhrase());
62
                $key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
63
64
                $result = $this->sftp->login($serverConfig->getUser(), $key);
65
66
                break;
67
68 View Code Duplication
            case Configuration::AUTH_BY_PEM_FILE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
69
70
                $key = new RSA();
71
                $key->loadKey(file_get_contents($serverConfig->getPemFile()));
72
                $result = $this->sftp->login($serverConfig->getUser(), $key);
73
74
                break;
75
76
            case Configuration::AUTH_BY_AGENT:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
77
78
                $key = new Agent();
79
                $key->startSSHForwarding(null);
80
                $result = $this->sftp->login($serverConfig->getUser(), $key);
81
82
                break;
83
84
            default:
85
                throw new RuntimeException('You need to specify authentication method.');
86
        }
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 View Code Duplication
    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
        if (!$this->sftp->put($remote, $local, SFTP::SOURCE_LOCAL_FILE)) {
136
            throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n"));
137
        }
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function download($local, $remote)
144
    {
145
        $this->checkConnection();
146
147
        if (!$this->sftp->get($remote, $local)) {
148
            throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n"));
149
        }
150
    }
151
152
    /**
153
     * @return Configuration
154
     */
155
    public function getConfiguration()
156
    {
157
        return $this->configuration;
158
    }
159
}
160