Completed
Push — master ( e3874c...094488 )
by Sebastian
02:41 queued 12s
created

CommandLine::pipeFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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;
11
12
use RuntimeException;
13
14
/**
15
 * Class CommandLine
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 CommandLine implements Command
23
{
24
    /**
25
     * List of system commands to execute
26
     *
27
     * @var \SebastianFeldmann\Cli\Command[]
28
     */
29
    private $commands = [];
30
31
    /**
32
     * Redirect the output
33
     *
34
     * @var string
35
     */
36
    private $redirectOutput;
37
38
    /**
39
     * Output pipeline
40
     *
41
     * @var \SebastianFeldmann\Cli\Command[]
42
     */
43
    private $pipeline = [];
44
45
    /**
46
     * Should 'pipefail' be set?
47
     *
48
     * @var bool
49
     */
50
    private $pipeFail = false;
51
52
    /**
53
     * List of acceptable exit codes.
54
     *
55
     * @var array
56
     */
57
    private $acceptedExitCodes = [0];
58
59
    /**
60
     * Set the list of accepted exit codes
61
     *
62
     * @param  int[] $codes
63
     * @return void
64
     */
65 1
    public function acceptExitCodes(array $codes) : void
66
    {
67 1
        $this->acceptedExitCodes = $codes;
68 1
    }
69
70
    /**
71
     * Redirect the stdOut.
72
     *
73
     * @param  string $path
74
     * @return void
75
     */
76 1
    public function redirectOutputTo($path) : void
77
    {
78 1
        $this->redirectOutput = $path;
79 1
    }
80
81
    /**
82
     * Should the output be redirected
83
     *
84
     * @return bool
85
     */
86 1
    public function isOutputRedirected() : bool
87
    {
88 1
        return !empty($this->redirectOutput);
89
    }
90
91
    /**
92
     * Redirect getter.
93
     *
94
     * @return string
95
     */
96 1
    public function getRedirectPath() : string
97
    {
98 1
        return $this->redirectOutput;
99
    }
100
101
    /**
102
     * Pipe the command into given command
103
     *
104
     * @param  \SebastianFeldmann\Cli\Command $cmd
105
     * @return void
106
     */
107 2
    public function pipeOutputTo(Command $cmd) : void
108
    {
109 2
        if (!$this->canPipe()) {
110
            throw new RuntimeException('Can\'t pipe output');
111
        }
112 2
        $this->pipeline[] = $cmd;
113 2
    }
114
115
    /**
116
     * Get the 'pipefail' option command snippet
117
     *
118
     * @return string
119
     */
120 4
    public function getPipeFail()
121
    {
122 4
        return ($this->isPiped() && $this->pipeFail) ? 'set -o pipefail; ' : '';
123
    }
124
125
    /**
126
     * Can the pipe '|' operator be used
127
     *
128
     * @return bool
129
     */
130 2
    public function canPipe() : bool
131
    {
132 2
        return !defined('PHP_WINDOWS_VERSION_BUILD');
133
    }
134
135
    /**
136
     * Is there a command pipeline
137
     *
138
     * @return bool
139
     */
140 4
    public function isPiped() : bool
141
    {
142 4
        return !empty($this->pipeline);
143
    }
144
145
    /**
146
     * Should the pipefail option be set
147
     *
148
     * @param bool $pipeFail
149
     */
150 1
    public function pipeFail(bool $pipeFail)
151
    {
152 1
        $this->pipeFail = $pipeFail;
153 1
    }
154
155
    /**
156
     * Return command pipeline
157
     *
158
     * @return string
159
     */
160 4
    public function getPipeline() : string
161
    {
162 4
        return $this->isPiped() ? ' | ' . implode(' | ', $this->pipeline) : '';
163
    }
164
165
    /**
166
     * Adds a cli command to list of commands to execute
167
     *
168
     * @param  \SebastianFeldmann\Cli\Command
169
     * @return void
170
     */
171 5
    public function addCommand(Command $cmd) : void
172
    {
173 5
        $this->commands[] = $cmd;
174 5
    }
175
176
    /**
177
     * Generates the system command
178
     *
179
     * @return string
180
     */
181 5
    public function getCommand() : string
182
    {
183 5
        $amount = count($this->commands);
184 5
        if ($amount < 1) {
185 1
            throw new RuntimeException('no command to execute');
186
        }
187 4
        $cmd = $this->getPipeFail()
188 4
             . ($amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0])
189 4
             . $this->getPipeline()
190 4
             . (!empty($this->redirectOutput) ? ' > ' . $this->redirectOutput : '');
191
192 4
        return $cmd;
193
    }
194
195
    /**
196
     * Returns a list of exit codes that are valid
197
     *
198
     * @return array
199
     */
200 1
    public function getAcceptableExitCodes() : array
201
    {
202 1
        return $this->acceptedExitCodes;
203
    }
204
205
    /**
206
     * Returns the command to execute
207
     *
208
     * @return string
209
     */
210 1
    public function __toString() : string
211
    {
212 1
        return $this->getCommand();
213
    }
214
}
215