Completed
Push — master ( 2c1fd1...4849ce )
by Greg
01:34
created

src/Transport/SshTransport.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Consolidation\SiteProcess\Transport;
4
5
use Consolidation\SiteProcess\SiteProcess;
6
use Consolidation\SiteProcess\Util\Escape;
7
use Consolidation\SiteAlias\SiteAliasInterface;
8
use Consolidation\SiteProcess\Util\Shell;
9
use Consolidation\Config\ConfigInterface;
10
11
/**
12
 * SshTransport knows how to wrap a command such that it runs on a remote
13
 * system via the ssh cli.
14
 */
15
class SshTransport implements TransportInterface
16
{
17
    protected $tty;
18
    protected $siteAlias;
19
20
    public function __construct(SiteAliasInterface $siteAlias)
21
    {
22
        $this->siteAlias = $siteAlias;
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function configure(SiteProcess $process)
29
    {
30
        $this->tty = $process->isTty();
31
    }
32
33
    /**
34
     * inheritdoc
35
     */
36 View Code Duplication
    public function wrap($args)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $transport = ['ssh'];
39
        $transportOptions = $this->getTransportOptions();
40
        $commandToExecute = $this->getCommandToExecute($args);
41
42
        return array_merge(
43
            $transport,
44
            $transportOptions,
45
            $commandToExecute
46
        );
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function addChdir($cd_remote, $args)
53
    {
54
        return array_merge(
55
            [
56
                'cd',
57
                $cd_remote,
58
                Shell::op('&&'),
59
            ],
60
            $args
61
        );
62
    }
63
64
    /**
65
     * getTransportOptions returns the transport options for the tranport
66
     * mechanism itself
67
     */
68
    protected function getTransportOptions()
69
    {
70
        $transportOptions = [
71
            Shell::preEscaped($this->siteAlias->get('ssh.options', '-o PasswordAuthentication=no')),
72
            $this->siteAlias->remoteHostWithUser(),
73
        ];
74
        if ($this->tty) {
75
            array_unshift($transportOptions, '-t');
76
        }
77
        return $transportOptions;
78
    }
79
80
    /**
81
     * getCommandToExecute processes the arguments for the command to
82
     * be executed such that they are appropriate for the transport mechanism.
83
     */
84
    protected function getCommandToExecute($args)
85
    {
86
        // Escape each argument for the target system and then join
87
        $args = Escape::argsForSite($this->siteAlias, $args);
88
        $commandToExecute = implode(' ', $args);
89
90
        return [$commandToExecute];
91
    }
92
}
93