Connection::authenticate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace LibSSH2;
3
4
use LibSSH2\Authentication\Authentication;
5
6
/**
7
 * Connection class.
8
 *
9
 * Remote resource connection setter class.
10
 *
11
 * @package LibSSH2
12
 */
13
class Connection extends ConsoleOutput
14
{
15
    /**
16
     * Remote subsystem connection resource.
17
     *
18
     * @var resource
19
     */
20
    protected $connection;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param  instance $configuration  Configuration class instance
26
     * @param  instance $authentication Authentication class instance
27
     * @param  boolean  $tunnel         require SSH tunnel
28
     * @return void
29
     */
30
    public function __construct(Configuration $configuration, Authentication $authentication)
31
    {
32
        if (extension_loaded('ssh2') == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
33
        {
34
            throw new \RuntimeException('The libssh2 extension is not loaded.');
35
        }
36
37
        $this->connect($configuration);
38
        $this->authenticate($authentication);
39
40
        if ($configuration->get_tunnel())
41
        {
42
            $this->tunnel($configuration);
43
        }
44
    }
45
46
    /**
47
     * Destructor.
48
     *
49
     * @return void
50
     */
51
    public function __destruct()
52
    {
53
        $this->disconnect();
54
    }
55
56
    /**
57
     * Create remote connection resource.
58
     *
59
     * @param  resource $configuration \LibSSH2\Configuration object
60
     * @return void
61
     */
62
    final public function connect(Configuration $configuration)
63
    {
64
        $this->connection = @ssh2_connect($configuration->get_host(), $configuration->get_port(), $configuration->get_methods());
65
        if ($this->connection === false || !is_resource($this->connection))
66
        {
67
            throw new \RuntimeException($this->get_error_message());
68
        }
69
        return;
70
    }
71
72
    /**
73
     * Create remote tunnel connection resource.
74
     *
75
     * @param  string $host hostname
76
     * @param  int    $port port (default=22)
77
     * @return void
78
     */
79
    final public function tunnel(Configuration $configuration)
80
    {
81
        $tunnel = @ssh2_tunnel($this->connection, $configuration->get_tunnel_host(), $configuration->get_tunnel_port());
82
        if ($tunnel === false)
0 ignored issues
show
introduced by
The condition $tunnel === false can never be true.
Loading history...
83
        {
84
            throw new \RuntimeException($this->get_error_message());
85
        }
86
        return;
87
    }
88
89
    /**
90
     * Authenticate remote connection resource.
91
     *
92
     * @param  resource $authentication \LibSSH2\Authentication\Authentication interface object
93
     * @return void
94
     */
95
    final public function authenticate(Authentication $authentication)
96
    {
97
        $authentication->authenticate($this->connection);
98
    }
99
    
100
    /**
101
     * {@inheritdoc}
102
     */
103
    final public function disconnect()
104
    {
105
        @ssh2_exec($this->connection, 'exit');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for ssh2_exec(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

105
        /** @scrutinizer ignore-unhandled */ @ssh2_exec($this->connection, 'exit');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
106
        unset($this->connection);
107
    }
108
}
109