Completed
Push — master ( 5735e1...ecb9c5 )
by Sebastian
02:27
created

Executable   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 150
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCommand() 0 7 4
A silence() 0 5 2
A addOption() 0 15 3
A addOptionIfNotEmpty() 0 11 3
A addArgument() 0 5 1
A escapeArgument() 0 10 2
A __toString() 0 4 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 Cmd
16
 *
17
 * @package SebastianFeldmann\Cli
18
 */
19
class Executable implements Command
20
{
21
    /**
22
     * Command name
23
     *
24
     * @var string
25
     */
26
    private $cmd;
27
28
    /**
29
     * Display stdErr
30
     *
31
     * @var boolean
32
     */
33
    private $isSilent = false;
34
35
    /**
36
     * Command options
37
     *
38
     * @var string[]
39
     */
40
    private $options = [];
41
42
    /**
43
     * Command arguments
44
     *
45
     * @var string[]
46
     */
47
    private $arguments;
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param string $cmd
53
     */
54 10
    public function __construct($cmd)
55
    {
56 10
        $this->cmd = $cmd;
57 10
    }
58
59
    /**
60
     * Returns the string to execute on the command line.
61
     *
62
     * @return string
63
     */
64 9
    public function getCommand() : string
65
    {
66 9
        return $this->cmd
67 9
        . (count($this->options)   ? ' ' . implode(' ', $this->options)   : '')
68 9
        . (count($this->arguments) ? ' ' . implode(' ', $this->arguments) : '')
69 9
        . ($this->isSilent         ? ' 2> /dev/null'                      : '');
70
    }
71
72
    /**
73
     * Silence the 'Cmd' by redirecting its stdErr output to /dev/null.
74
     * The silence feature is disabled for Windows systems.
75
     *
76
     * @param  bool $bool
77
     * @return \SebastianFeldmann\Cli\Command\Executable
78
     */
79 1
    public function silence($bool = true) : Executable
80
    {
81 1
        $this->isSilent = $bool && !defined('PHP_WINDOWS_VERSION_BUILD');
82 1
        return $this;
83
    }
84
85
    /**
86
     * Add option to list.
87
     *
88
     * @param  string               $option
89
     * @param  mixed <string|array> $value
90
     * @param  string               $glue
91
     * @return \SebastianFeldmann\Cli\Command\Executable
92
     */
93 3
    public function addOption(string $option, $value = null, string $glue = '=') : Executable
94
    {
95 3
        if ($value !== null) {
96
            // force space for multiple arguments e.g. --option 'foo' 'bar'
97 2
            if (is_array($value)) {
98 1
                $glue = ' ';
99
            }
100 2
            $value = $glue . $this->escapeArgument($value);
101
        } else {
102 1
            $value = '';
103
        }
104 3
        $this->options[] = $option . $value;
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * Adds an option to a command if it is not empty.
111
     *
112
     * @param  string $option
113
     * @param  mixed  $check
114
     * @param  bool   $asValue
115
     * @param  string $glue
116
     * @return \SebastianFeldmann\Cli\Command\Executable
117
     */
118 2
    public function addOptionIfNotEmpty(string $option, $check, bool $asValue = true, string $glue = '=') : Executable
119
    {
120 2
        if (!empty($check)) {
121 2
            if ($asValue) {
122 1
                $this->addOption($option, $check, $glue);
123
            } else {
124 1
                $this->addOption($option);
125
            }
126
        }
127 2
        return $this;
128
    }
129
130
    /**
131
     * Add argument to list.
132
     *
133
     * @param  mixed <string|array> $argument
134
     * @return \SebastianFeldmann\Cli\Command\Executable
135
     */
136 5
    public function addArgument($argument) : Executable
137
    {
138 5
        $this->arguments[] = $this->escapeArgument($argument);
139 5
        return $this;
140
    }
141
142
    /**
143
     * Escape a shell argument.
144
     *
145
     * @param  mixed <string|array> $argument
146
     * @return string
147
     */
148 7
    protected function escapeArgument($argument) : string
149
    {
150 7
        if (is_array($argument)) {
151 2
            $argument = array_map('escapeshellarg', $argument);
152 2
            $escaped  = implode(' ', $argument);
153
        } else {
154 5
            $escaped = escapeshellarg($argument);
155
        }
156 7
        return $escaped;
157
    }
158
159
    /**
160
     * Returns the command to execute.
161
     *
162
     * @return string
163
     */
164 9
    public function __toString() : string
165
    {
166 9
        return $this->getCommand();
167
    }
168
}
169