Completed
Pull Request — master (#25)
by Moshe
01:21
created

SiteProcess::chdirToSiteRoot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
namespace Consolidation\SiteProcess;
3
4
use Consolidation\SiteAlias\AliasRecord;
5
use Consolidation\SiteProcess\Transport\DockerComposeTransport;
6
use Consolidation\SiteProcess\Util\ArgumentProcessor;
7
use Consolidation\SiteProcess\Transport\LocalTransport;
8
use Consolidation\SiteProcess\Transport\SshTransport;
9
use Consolidation\SiteProcess\Transport\TransportInterface;
10
use Consolidation\Config\Util\Interpolator;
11
use Consolidation\SiteProcess\Util\ShellOperatorInterface;
12
use Consolidation\SiteProcess\Util\Escape;
13
14
/**
15
 * A wrapper around Symfony Process that uses site aliases
16
 * (https://github.com/consolidation/site-alias)
17
 *
18
 * - Interpolate arguments using values from the alias
19
 *   e.g. `$process = new SiteProcess($alias, ['git', '-C', '{{root}}']);`
20
 * - Make remote calls via ssh as if they were local.
21
 */
22
class SiteProcess extends ProcessBase
23
{
24
    /** @var AliasRecord */
25
    protected $siteAlias;
26
    /** @var string[] */
27
    protected $args;
28
    /** @var string[] */
29
    protected $options;
30
    /** @var string[] */
31
    protected $optionsPassedAsArgs;
32
    /** @var string */
33
    protected $cd;
34
    /** @var TransportInterface */
35
    protected $transport;
36
37
    /**
38
     * Process arguments and options per the site alias and build the
39
     * actual command to run.
40
     */
41
    public function __construct(AliasRecord $siteAlias, TransportInterface $transport, $args, $options = [], $optionsPassedAsArgs = [])
42
    {
43
        $this->siteAlias = $siteAlias;
44
        $this->transport = $transport;
45
        $this->args = $args;
46
        $this->options = $options;
47
        $this->optionsPassedAsArgs = $optionsPassedAsArgs;
48
49
        parent::__construct([]);
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getCd()
56
    {
57
        return $this->cd;
58
    }
59
60
    /**
61
     * @param string $cwd
0 ignored issues
show
Bug introduced by
There is no parameter named $cwd. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     */
63
    public function setCd($cd)
64
    {
65
        $this->cd = $cd;
66
    }
67
68
    /**
69
     *
70
     * @param bool $shouldUseSiteRoot
71
     * @return $this|\Symfony\Component\Process\Process
72
     * @throws \Exception
73
     * @deprecated.
74
     */
75
    public function chdirToSiteRoot($shouldUseSiteRoot = true)
76
    {
77
        if (!$shouldUseSiteRoot || !$this->siteAlias->hasRoot()) {
78
            return $this;
79
        }
80
81
        return $this->setWorkingDirectory($this->siteAlias->root());
82
    }
83
84
    /**
85
     * Take all of our individual arguments and process them for use.
86
     */
87
    protected function processArgs()
88
    {
89
        $transport = $this->getTransport($this->siteAlias);
90
        $transport->configure($this);
91
92
        $processor = new ArgumentProcessor();
93
        $selectedArgs = $processor->selectArgs(
94
            $this->siteAlias,
95
            $this->args,
96
            $this->options,
97
            $this->optionsPassedAsArgs
98
        );
99
100
        // Ask the transport to drop in a 'cd' if needed.
101
        if ($this->cd) {
102
            $selectedArgs = $transport->addChdir($this->cd, $selectedArgs, $this);
103
        }
104
105
        // Do any necessary interpolation on the selected arguments.
106
        $processedArgs = $this->interpolate($selectedArgs);
107
108
        // Wrap the command with 'ssh' or some other transport if this is
109
        // a remote command; otherwise, leave it as-is.
110
        return $transport->wrap($processedArgs);
111
    }
112
113
    public function setTransport($transport)
114
    {
115
        $this->transport = $transport;
116
    }
117
118
    /**
119
     * Ask the transport manager for the correct transport for the
120
     * provided alias.
121
     */
122
    protected function getTransport(AliasRecord $siteAlias)
0 ignored issues
show
Unused Code introduced by
The parameter $siteAlias is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        return $this->transport;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function getCommandLine()
131
    {
132
        $commandLine = parent::getCommandLine();
133
        if (empty($commandLine)) {
134
            $processedArgs = $this->processArgs();
135
            $commandLine = Escape::argsForSite($this->siteAlias, $processedArgs);
136
            $commandLine = implode(' ', $commandLine);
137
            $this->setCommandLine($commandLine);
138
        }
139
        return $commandLine;
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function start(callable $callback = null, $env = array())
146
    {
147
        $cmd = $this->getCommandLine();
0 ignored issues
show
Unused Code introduced by
$cmd is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
148
        parent::start($callback, $env);
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function wait(callable $callback = null)
155
    {
156
        $return = parent::wait($callback);
157
        return $return;
158
    }
159
160
    /**
161
     * interpolate examines each of the arguments in the provided argument list
162
     * and replaces any token found therein with the value for that key as
163
     * pulled from the given site alias.
164
     *
165
     * Example: "git -C {{root}} status"
166
     *
167
     * The token "{{root}}" will be converted to a value via $siteAlias->get('root').
168
     * The result will replace the token.
169
     *
170
     * It is possible to use dot notation in the keys to access nested elements
171
     * within the site alias record.
172
     *
173
     * @param AliasRecord $siteAlias
0 ignored issues
show
Bug introduced by
There is no parameter named $siteAlias. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
174
     * @param type $args
175
     * @return type
176
     */
177
    protected function interpolate($args)
178
    {
179
        $interpolator = new Interpolator();
180
        return array_map(
181
            function ($arg) use ($interpolator) {
182
                if ($arg instanceof ShellOperatorInterface) {
183
                    return $arg;
184
                }
185
                return $interpolator->interpolate($this->siteAlias, $arg, false);
186
            },
187
            $args
188
        );
189
    }
190
}
191