Completed
Push — master ( 8ccd6b...240592 )
by Sebastian
02:32
created

CommandLine::getAcceptableExitCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
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;
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
     * List of acceptable exit codes.
47
     *
48
     * @var array
49
     */
50
    private $acceptedExitCodes = [0];
51
52
    /**
53
     * Set the list of accepted exit codes.
54
     *
55
     * @param  int[] $codes
56
     * @return void
57
     */
58 1
    public function acceptExitCodes(array $codes) : void
59
    {
60 1
        $this->acceptedExitCodes = $codes;
61 1
    }
62
63
    /**
64
     * Redirect the stdOut.
65
     *
66
     * @param  string $path
67
     * @return void
68
     */
69 1
    public function redirectOutputTo($path) : void
70
    {
71 1
        $this->redirectOutput = $path;
72 1
    }
73
74
    /**
75
     * Should the output be redirected.
76
     *
77
     * @return bool
78
     */
79 1
    public function isOutputRedirected() : bool
80
    {
81 1
        return !empty($this->redirectOutput);
82
    }
83
84
    /**
85
     * Redirect getter.
86
     *
87
     * @return string
88
     */
89 1
    public function getRedirectPath() : string
90
    {
91 1
        return $this->redirectOutput;
92
    }
93
94
    /**
95
     * Pipe the command into given command.
96
     *
97
     * @param  \SebastianFeldmann\Cli\Command $cmd
98
     * @return void
99
     */
100 1
    public function pipeOutputTo(Command $cmd) : void
101
    {
102 1
        if (!$this->canPipe()) {
103
            throw new RuntimeException('Can\'t pipe output');
104
        }
105 1
        $this->pipeline[] = $cmd;
106 1
    }
107
108
    /**
109
     * Can the pipe '|' operator be used.
110
     *
111
     * @return bool
112
     */
113 1
    public function canPipe() : bool
114
    {
115 1
        return !defined('PHP_WINDOWS_VERSION_BUILD');
116
    }
117
118
    /**
119
     * Is there a command pipeline.
120
     *
121
     * @return bool
122
     */
123 3
    public function isPiped() : bool
124
    {
125 3
        return !empty($this->pipeline);
126
    }
127
128
    /**
129
     * Return command pipeline.
130
     *
131
     * @return string
132
     */
133 3
    public function getPipeline() : string
134
    {
135 3
        return $this->isPiped() ? ' | ' . implode(' | ', $this->pipeline) : '';
136
    }
137
138
    /**
139
     * Adds a cli command to list of commands to execute.
140
     *
141
     * @param  \SebastianFeldmann\Cli\Command
142
     * @return void
143
     */
144 4
    public function addCommand(Command $cmd) : void
145
    {
146 4
        $this->commands[] = $cmd;
147 4
    }
148
149
    /**
150
     * Generates the system command.
151
     *
152
     * @return string
153
     */
154 4
    public function getCommand() : string
155
    {
156 4
        $amount = count($this->commands);
157 4
        if ($amount < 1) {
158 1
            throw new RuntimeException('no command to execute');
159
        }
160 3
        $cmd = ($amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0])
161 3
             . $this->getPipeline()
162 3
             . (!empty($this->redirectOutput) ? ' > ' . $this->redirectOutput : '');
163
164 3
        return $cmd;
165
    }
166
167
    /**
168
     * Returns a list of exit codes that are valid.
169
     *
170
     * @return array
171
     */
172 1
    public function getAcceptableExitCodes() : array
173
    {
174 1
        return $this->acceptedExitCodes;
175
    }
176
177
    /**
178
     * Returns the command to execute.
179
     *
180
     * @return string
181
     */
182 1
    public function __toString() : string
183
    {
184 1
        return $this->getCommand();
185
    }
186
}
187