Ssh::getConnection()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 0
1
<?php
2
3
namespace Fabrica\Tools;
4
5
use phpseclib3\Net\SSH2;
6
7
use Fabrica\Models\Infra\Computer;
8
9
/**
10
 * Bash Class
11
 *
12
 * @class Bash
13
 */
14
class Ssh extends Bash
15
{
16
17
    protected $computer = false;
18
19
    public function __construct(Computer $computer)
20
    {
21
        $this->computer = $computer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $computer of type object<Fabrica\Models\Infra\Computer> is incompatible with the declared type boolean of property $computer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
22
    }
23
24
    public function getConnection()
25
    {
26
        if (!$this->connection) {
27
            $this->connection = new SSH2($this->computer->host);
0 ignored issues
show
Bug introduced by
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
            if ($this->computer->key) {
29
                if ($this->connection->login($this->computer->username, $this->computer->key->getKey())) {
30
                    return $this->connection;
31
                }
32
            }
33
34
            if (isset($this->computer->password) && $this->computer->password && $this->connection->login($this->computer->username, $this->computer->password)) {
35
                return $this->connection;
36
            }
37
            
38
            exit('Login Failed');
39
        }
40
        return $this->connection;
41
    }
42
43
    public function exec($exec, $path = false)
44
    {
45
        if (!$path) {
46
            $exec = 'cd '.$this->computer.' && '.$exec;
47
        }
48
        
49
        return $this->getConnection()->exec($exec);
50
    }
51
}
52