Completed
Pull Request — master (#22)
by
unknown
02:38
created

AbstractAnsibleCommand::addParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the php-ansible package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Asm\Ansible\Command;
12
13
use Symfony\Component\Process\ProcessBuilder;
14
15
/**
16
 * Class AbstractAnsibleCommand
17
 *
18
 * @package Asm\Ansible\Command
19
 * @author Marc Aschmann <[email protected]>
20
 */
21
abstract class AbstractAnsibleCommand
22
{
23
    /**
24
     * @var ProcessBuilder
25
     */
26
    protected $processBuilder;
27
28
    /**
29
     * @var array
30
     */
31
    private $options;
32
33
    /**
34
     * @var array
35
     */
36
    private $parameters;
37
38
    /**
39
     * @var array
40
     */
41
    private $baseOptions;
42
43
    /**
44
     * @param ProcessBuilder $processBuilder
45
     */
46 13
    public function __construct(ProcessBuilder $processBuilder)
47
    {
48 13
        $this->processBuilder = $processBuilder;
49 13
        $this->options = [];
50 13
        $this->parameters = [];
51 13
        $this->baseOptions = [];
52 13
    }
53
54
    /**
55
     * Get parameter string which will be used to call ansible.
56
     *
57
     * @param bool $asArray
58
     * @return string[]
59
     */
60 43
    protected function prepareArguments($asArray = true)
61
    {
62 43
        $arguments = array_merge(
63 43
            [$this->getBaseOptions()],
64 43
            $this->getOptions(),
65 43
            $this->getParameters()
66 43
        );
67
68 43
        if (false === $asArray) {
69 6
            $arguments = implode(' ', $arguments);
70 6
        }
71
72 43
        return $arguments;
73
    }
74
75
    /**
76
     * Add an Option.
77
     *
78
     * @param string $name
79
     * @param string $value
80
     */
81 16
    protected function addOption($name, $value)
82
    {
83 16
        $this->options[$name] = $value;
84 16
    }
85
86
    /**
87
     * Add a parameter.
88
     *
89
     * @param string $name
90
     */
91 19
    protected function addParameter($name)
92
    {
93 19
        $this->parameters[] = $name;
94 19
    }
95
96
    /**
97
     * Get all options as array.
98
     *
99
     * @return string
100
     */
101 43
    protected function getOptions()
102
    {
103 43
        $options = [];
104
105 43
        foreach ($this->options as $name => $value) {
106 33
            $options[] = $name . '=' . $value;
107 43
        }
108
109 43
        return $options;
110
    }
111
112
    /**
113
     * Get all parameters as array.
114
     *
115
     * @return string
116
     */
117 43
    protected function getParameters()
118
    {
119 43
        return $this->parameters;
120
    }
121
122
123
    /**
124
     * Add base options to internal storage.
125
     *
126
     * @param string $baseOption
127
     * @return $this
128
     */
129 41
    protected function addBaseoption($baseOption)
130
    {
131 41
        $this->baseOptions[] = $baseOption;
132
133 41
        return $this;
134
    }
135
136
    /**
137
     * Generate base options string.
138
     *
139
     * @return string
140
     */
141 43
    protected function getBaseOptions()
142
    {
143 43
        return implode(' ', $this->baseOptions);
144
    }
145
146
    /**
147
     * Check if param is array or string and implode with glue if necessary.
148
     *
149
     * @param string|array $param
150
     * @param string $glue
151
     * @return string
152
     */
153 9
    protected function checkParam($param, $glue = ' ')
154
    {
155 9
        if (is_array($param)) {
156 6
            $param = implode($glue, $param);
157 6
        }
158
159 9
        return $param;
160
    }
161
162
    /**
163
     * Creates process with process builder and executes it.
164
     *
165
     * @param null $callback
166
     * @return int|string
167
     */
168 2
    protected function runProcess($callback)
169
    {
170 2
        $process = $this->processBuilder
171 2
            ->setArguments(
172 2
                $this->prepareArguments()
1 ignored issue
show
Documentation introduced by
$this->prepareArguments() is of type string|array, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
173 2
            )
174 2
            ->getProcess();
175
176
        // exitcode
177 2
        $result = $process->run($callback);
178
179
        // text-mode
180 2
        if (null === $callback) {
181 1
            $result = $process->getOutput();
182
183 1
            if (false === $process->isSuccessful()) {
184 1
                $process->getErrorOutput();
185 1
            }
186 1
        }
187
188 2
        return $result;
189
    }
190
}
191