Completed
Push — master ( 6eea0c...660ba2 )
by Sebastian
01:22
created

Executable::getAcceptableExitCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Cli.
4
 *
5
 * (c) Sebastian Feldmann <[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
namespace SebastianFeldmann\Cli\Command;
11
12
use SebastianFeldmann\Cli\Command;
13
14
/**
15
 * Class Executable
16
 *
17
 * @package SebastianFeldmann\Cli
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/cli
20
 * @since   Class available since Release 0.9.0
21
 */
22
class Executable implements Command
23
{
24
    /**
25
     * Command name
26
     *
27
     * @var string
28
     */
29
    private $cmd;
30
31
    /**
32
     * Display stdErr
33
     *
34
     * @var boolean
35
     */
36
    private $isSilent = false;
37
38
    /**
39
     * Command options
40
     *
41
     * @var string[]
42
     */
43
    private $options = [];
44
45
    /**
46
     * List of acceptable exit codes.
47
     *
48
     * @var array
49
     */
50
    private $acceptableExitCodes = [];
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param string $cmd
56
     * @param int[]  $exitCodes
57
     */
58 11
    public function __construct(string $cmd, array $exitCodes = [0])
59
    {
60 11
        $this->cmd                 = $cmd;
61 11
        $this->acceptableExitCodes = $exitCodes;
62 11
    }
63
64
    /**
65
     * Returns the string to execute on the command line.
66
     *
67
     * @return string
68
     */
69 9
    public function getCommand() : string
70
    {
71 9
        return $this->cmd
72 9
        . (count($this->options)   ? ' ' . implode(' ', $this->options)   : '')
73 9
        . ($this->isSilent         ? ' 2> /dev/null'                      : '');
74
    }
75
76
    /**
77
     * Returns a list of exit codes that are valid.
78
     *
79
     * @return int[]
80
     */
81 1
    public function getAcceptableExitCodes() : array
82
    {
83 1
        return $this->acceptableExitCodes;
84
    }
85
86
    /**
87
     * Silence the 'Cmd' by redirecting its stdErr output to /dev/null.
88
     * The silence feature is disabled for Windows systems.
89
     *
90
     * @param  bool $bool
91
     * @return \SebastianFeldmann\Cli\Command\Executable
92
     */
93 1
    public function silence($bool = true) : Executable
94
    {
95 1
        $this->isSilent = $bool && !defined('PHP_WINDOWS_VERSION_BUILD');
96 1
        return $this;
97
    }
98
99
    /**
100
     * Add option to list.
101
     *
102
     * @param  string               $option
103
     * @param  mixed <string|array> $value
104
     * @param  string               $glue
105
     * @return \SebastianFeldmann\Cli\Command\Executable
106
     */
107 3
    public function addOption(string $option, $value = null, string $glue = '=') : Executable
108
    {
109 3
        if ($value !== null) {
110
            // force space for multiple arguments e.g. --option 'foo' 'bar'
111 2
            if (is_array($value)) {
112 1
                $glue = ' ';
113
            }
114 2
            $value = $glue . $this->escapeArgument($value);
115
        } else {
116 1
            $value = '';
117
        }
118 3
        $this->options[] = $option . $value;
119
120 3
        return $this;
121
    }
122
123
    /**
124
     * Adds an option to a command if it is not empty.
125
     *
126
     * @param  string $option
127
     * @param  mixed  $check
128
     * @param  bool   $asValue
129
     * @param  string $glue
130
     * @return \SebastianFeldmann\Cli\Command\Executable
131
     */
132 2
    public function addOptionIfNotEmpty(string $option, $check, bool $asValue = true, string $glue = '=') : Executable
133
    {
134 2
        if (!empty($check)) {
135 2
            if ($asValue) {
136 1
                $this->addOption($option, $check, $glue);
137
            } else {
138 1
                $this->addOption($option);
139
            }
140
        }
141 2
        return $this;
142
    }
143
144
    /**
145
     * Add argument to list.
146
     *
147
     * @param  mixed <string|array> $argument
148
     * @return \SebastianFeldmann\Cli\Command\Executable
149
     */
150 6
    public function addArgument($argument) : Executable
151
    {
152 6
        $this->options[] = $this->escapeArgument($argument);
153 6
        return $this;
154
    }
155
156
    /**
157
     * Escape a shell argument.
158
     *
159
     * @param  mixed <string|array> $argument
160
     * @return string
161
     */
162 8
    protected function escapeArgument($argument) : string
163
    {
164 8
        if (is_array($argument)) {
165 2
            $argument = array_map('escapeshellarg', $argument);
166 2
            $escaped  = implode(' ', $argument);
167
        } else {
168 6
            $escaped = escapeshellarg($argument);
169
        }
170 8
        return $escaped;
171
    }
172
173
    /**
174
     * Returns the command to execute.
175
     *
176
     * @return string
177
     */
178 9
    public function __toString() : string
179
    {
180 9
        return $this->getCommand();
181
    }
182
}
183