SshClient::getSftp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Dock\Docker\Machine;
4
5
use Ssh\Authentication\Password;
6
use Ssh\Configuration;
7
use Ssh\Exec;
8
use Ssh\Session;
9
10
class SshClient
11
{
12
    const DEFAULT_USERNAME = 'docker';
13
    const DEFAULT_PASSWORD = 'tcuser';
14
15
    /**
16
     * @var \Ssh\Session
17
     */
18
    private $session;
19
20
    /**
21
     * @var Exec
22
     */
23
    private $exec;
24
25
    /**
26
     * @var Machine
27
     */
28
    private $machine;
29
30
    /**
31
     * @param Machine $machine
32
     */
33
    public function __construct(Machine $machine)
34
    {
35
        $this->machine = $machine;
36
    }
37
38
    /**
39
     * @param string $command
40
     *
41
     * @return string
42
     */
43
    public function run($command)
44
    {
45
        if (!$this->exec) {
46
            $this->exec = $this->getSession()->getExec();
47
        }
48
49
        return $this->exec->run($command, null, []);
50
    }
51
52
    /**
53
     * @param string $command
54
     *
55
     * @return bool
56
     */
57
    public function runAndCheckOutputWasGenerated($command)
58
    {
59
        try {
60
            $result = $this->run($command);
61
        } catch (\RuntimeException $e) {
62
            $result = null;
63
        }
64
65
        return !empty($result);
66
    }
67
68
    /**
69
     * @return \Ssh\Sftp
70
     */
71
    public function getSftp()
72
    {
73
        return $this->getSession()->getSftp();
74
    }
75
76
    /**
77
     * @return Session
78
     */
79
    private function getSession()
80
    {
81
        if (null == $this->session) {
82
            $this->session = new Session(
83
                new Configuration(
84
                    $this->machine->getIp()
85
                ),
86
                new Password(self::DEFAULT_USERNAME, self::DEFAULT_PASSWORD)
87
            );
88
        }
89
90
        return $this->session;
91
    }
92
}
93