Completed
Push — master ( 1e4e4c...8be129 )
by Marin
05:19
created

Ssh::getSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Anorgan\Deployer\Common\Server;
4
5
use Psr\Log\LoggerInterface;
6
use Ssh\Authentication\Agent;
7
use Ssh\Configuration;
8
use Ssh\Session;
9
use Symfony\Component\Process\Process;
10
11
class Ssh extends AbstractServer
12
{
13
    private $user;
14
15
    /**
16
     * @param string $title
17
     * @param string $path
18
     * @param string $hostname
19
     * @param $user
20
     * @param LoggerInterface $logger
21
     */
22
    public function __construct($title, $path, $hostname, $user, LoggerInterface $logger = null)
23
    {
24
        parent::__construct($title, $path, $logger);
25
26
        $this->setHostname($hostname);
27
        $this->user = $user;
28
    }
29
30
    public function runCommands()
31
    {
32
        $commands = ['cd '.$this->getPath()];
33
        $commands = array_merge($commands, $this->getCommands());
34
        $commands = implode('; ', $commands);
35
        $process  = new Process($commands);
36
37
        $commandLine = $process->getCommandLine();
38
        $this->logger->info('Running "'.$commandLine.'"');
39
        $output = $this->getSession()->getExec()->run($commandLine);
40
        $this->logger->info('Output:'.PHP_EOL.$output);
41
    }
42
43
    /**
44
     * @return Session
45
     */
46
    private function getSession()
47
    {
48
        $configuration  = new Configuration($this->getHostname());
49
        $authentication = new Agent($this->user);
50
        $session        = new Session($configuration, $authentication);
51
52
        return $session;
53
    }
54
}
55