Completed
Push — master ( 924f7d...1d4235 )
by Greg
99:30
created

SshTransport   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 4 1
A wrap() 0 12 1
A addChdir() 0 11 1
A getTransportOptions() 0 11 2
A getCommandToExecute() 0 7 1
1
<?php
2
3
namespace Consolidation\SiteProcess\Transport;
4
5
use Drush\Drush;
6
use Psr\Log\LoggerInterface;
7
use Robo\Common\IO;
8
use Symfony\Component\Console\Style\OutputStyle;
9
use Symfony\Component\Process\Process;
10
use Consolidation\SiteProcess\Util\RealtimeOutputHandler;
11
use Symfony\Component\Console\Output\ConsoleOutputInterface;
12
use Consolidation\SiteAlias\AliasRecord;
13
14
/**
15
 * SshTransport knows how to wrap a command such that it runs on a remote
16
 * system via the ssh cli.
17
 */
18
class SshTransport implements TransportInterface
19
{
20
    protected $tty;
21
22
    public function __construct(AliasRecord $siteAlias)
23
    {
24
        $this->siteAlias = $siteAlias;
0 ignored issues
show
Bug introduced by
The property siteAlias 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...
25
    }
26
27
    /**
28
     * inheritdoc
29
     */
30
    public function configure(Process $process)
31
    {
32
        $this->tty = $process->isTty();
33
    }
34
35
    /**
36
     * inheritdoc
37
     */
38
    public function wrap($args)
39
    {
40
        $transport = ['ssh'];
41
        $transportOptions = $this->getTransportOptions();
42
        $commandToExecute = $this->getCommandToExecute($args);
43
44
        return array_merge(
45
            $transport,
46
            $transportOptions,
47
            $commandToExecute
48
        );
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function addChdir($cd, $args)
55
    {
56
        return array_merge(
57
            [
58
                'cd',
59
                $cd,
60
                '&&',
61
            ],
62
            $args
63
        );
64
    }
65
66
    /**
67
     * getTransportOptions returns the transport options for the tranport
68
     * mechanism itself
69
     */
70
    protected function getTransportOptions()
71
    {
72
        $transportOptions = [
73
            $this->siteAlias->get('ssh.options', '-o PasswordAuthentication=no'),
74
            $this->siteAlias->remoteHostWithUser(),
75
        ];
76
        if ($this->tty) {
77
            array_unshift($transportOptions, '-t');
78
        }
79
        return $transportOptions;
80
    }
81
82
    /**
83
     * getCommandToExecute processes the arguments for the command to
84
     * be executed such that they are appropriate for the transport mechanism.
85
     */
86
    protected function getCommandToExecute($args)
87
    {
88
        // @TODO: Methinks this needs to be escaped for the target system.
89
        $commandToExecute = implode(' ', $args);
90
91
        return [$commandToExecute];
92
    }
93
}
94