Completed
Pull Request — master (#146)
by Alexander
02:39
created

ProcessBuilder   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 228
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A add() 0 5 1
A setPrefix() 0 5 2
A setArguments() 0 5 1
A setWorkingDirectory() 0 5 1
A inheritEnvironmentVariables() 0 5 1
A setEnv() 0 5 1
A addEnvironmentVariables() 0 5 1
A setInput() 0 5 1
A setTimeout() 0 13 3
A setOption() 0 5 1
A disableOutput() 0 5 1
A enableOutput() 0 5 1
B getProcess() 0 21 7
1
<?php
2
/*
3
 * This file was taken from the "symfony/process" for backward compatibility.
4
 * https://github.com/symfony/process/tree/3.4
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Alchemy\Zippy\ProcessBuilder;
12
13
use \InvalidArgumentException;
14
use \LogicException;
15
use Symfony\Component\Process\Process;
16
17
class ProcessBuilder
18
{
19
    private $arguments;
20
    private $cwd;
21
    private $env = array();
22
    private $input;
23
    private $timeout = 60;
24
    private $options = array();
25
    private $inheritEnv = true;
26
    private $prefix = array();
27
    private $outputDisabled = false;
28
    /**
29
     * Constructor.
30
     *
31
     * @param string[] $arguments An array of arguments
32
     */
33
    public function __construct(array $arguments = array())
34
    {
35
        $this->arguments = $arguments;
36
    }
37
    /**
38
     * Creates a process builder instance.
39
     *
40
     * @param string[] $arguments An array of arguments
41
     *
42
     * @return static
43
     */
44
    public static function create(array $arguments = array())
45
    {
46
        return new static($arguments);
47
    }
48
    /**
49
     * Adds an unescaped argument to the command string.
50
     *
51
     * @param string $argument A command argument
52
     *
53
     * @return $this
54
     */
55
    public function add($argument)
56
    {
57
        $this->arguments[] = $argument;
58
        return $this;
59
    }
60
    /**
61
     * Adds a prefix to the command string.
62
     *
63
     * The prefix is preserved when resetting arguments.
64
     *
65
     * @param string|array $prefix A command prefix or an array of command prefixes
66
     *
67
     * @return $this
68
     */
69
    public function setPrefix($prefix)
70
    {
71
        $this->prefix = is_array($prefix) ? $prefix : array($prefix);
72
        return $this;
73
    }
74
    /**
75
     * Sets the arguments of the process.
76
     *
77
     * Arguments must not be escaped.
78
     * Previous arguments are removed.
79
     *
80
     * @param string[] $arguments
81
     *
82
     * @return $this
83
     */
84
    public function setArguments(array $arguments)
85
    {
86
        $this->arguments = $arguments;
87
        return $this;
88
    }
89
    /**
90
     * Sets the working directory.
91
     *
92
     * @param null|string $cwd The working directory
93
     *
94
     * @return $this
95
     */
96
    public function setWorkingDirectory($cwd)
97
    {
98
        $this->cwd = $cwd;
99
        return $this;
100
    }
101
    /**
102
     * Sets whether environment variables will be inherited or not.
103
     *
104
     * @param bool $inheritEnv
105
     *
106
     * @return $this
107
     */
108
    public function inheritEnvironmentVariables($inheritEnv = true)
109
    {
110
        $this->inheritEnv = $inheritEnv;
111
        return $this;
112
    }
113
    /**
114
     * Sets an environment variable.
115
     *
116
     * Setting a variable overrides its previous value. Use `null` to unset a
117
     * defined environment variable.
118
     *
119
     * @param string      $name  The variable name
120
     * @param null|string $value The variable value
121
     *
122
     * @return $this
123
     */
124
    public function setEnv($name, $value)
125
    {
126
        $this->env[$name] = $value;
127
        return $this;
128
    }
129
    /**
130
     * Adds a set of environment variables.
131
     *
132
     * Already existing environment variables with the same name will be
133
     * overridden by the new values passed to this method. Pass `null` to unset
134
     * a variable.
135
     *
136
     * @param array $variables The variables
137
     *
138
     * @return $this
139
     */
140
    public function addEnvironmentVariables(array $variables)
141
    {
142
        $this->env = array_replace($this->env, $variables);
143
        return $this;
144
    }
145
    /**
146
     * Sets the input of the process.
147
     *
148
     * @param resource|scalar|\Traversable|null $input The input content
149
     *
150
     * @return $this
151
     *
152
     * @throws InvalidArgumentException In case the argument is invalid
153
     */
154
    public function setInput($input)
155
    {
156
        $this->input = ProcessUtils::validateInput(__METHOD__, $input);
157
        return $this;
158
    }
159
    /**
160
     * Sets the process timeout.
161
     *
162
     * To disable the timeout, set this value to null.
163
     *
164
     * @param float|null $timeout
165
     *
166
     * @return $this
167
     *
168
     * @throws InvalidArgumentException
169
     */
170
    public function setTimeout($timeout)
171
    {
172
        if (null === $timeout) {
173
            $this->timeout = null;
174
            return $this;
175
        }
176
        $timeout = (float) $timeout;
177
        if ($timeout < 0) {
178
            throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
179
        }
180
        $this->timeout = $timeout;
0 ignored issues
show
Documentation Bug introduced by
The property $timeout was declared of type integer, but $timeout is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
181
        return $this;
182
    }
183
    /**
184
     * Adds a proc_open option.
185
     *
186
     * @param string $name  The option name
187
     * @param string $value The option value
188
     *
189
     * @return $this
190
     */
191
    public function setOption($name, $value)
192
    {
193
        $this->options[$name] = $value;
194
        return $this;
195
    }
196
    /**
197
     * Disables fetching output and error output from the underlying process.
198
     *
199
     * @return $this
200
     */
201
    public function disableOutput()
202
    {
203
        $this->outputDisabled = true;
204
        return $this;
205
    }
206
    /**
207
     * Enables fetching output and error output from the underlying process.
208
     *
209
     * @return $this
210
     */
211
    public function enableOutput()
212
    {
213
        $this->outputDisabled = false;
214
        return $this;
215
    }
216
    /**
217
     * Creates a Process instance and returns it.
218
     *
219
     * @return Process
220
     *
221
     * @throws LogicException In case no arguments have been provided
222
     */
223
    public function getProcess()
224
    {
225
        if (0 === count($this->prefix) && 0 === count($this->arguments)) {
226
            throw new LogicException('You must add() command arguments before calling getProcess().');
227
        }
228
        $options = $this->options;
229
        $arguments = array_merge($this->prefix, $this->arguments);
230
        $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
231
        $process = new Process($script, $this->cwd, $this->env, $this->input, $this->timeout, $options);
0 ignored issues
show
Unused Code introduced by
The call to Process::__construct() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
232
        if ($this->inheritEnv) {
233
            if (method_exists($process, 'inheritEnvironmentVariables')) {
234
                $process->inheritEnvironmentVariables();
235
            }
236
        }
237
        if ($this->outputDisabled) {
238
            if (method_exists($process, 'disableOutput')) {
239
                $process->disableOutput();
240
            }
241
        }
242
        return $process;
243
    }
244
}
245