SshTransport   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 25.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 20
loc 78
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() 12 12 1
A addChdir() 0 11 1
A getTransportOptions() 0 11 2
A getCommandToExecute() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
Duplication introduced by
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 View Code Duplication
    protected function getCommandToExecute($args)
0 ignored issues
show
Duplication introduced by
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...
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