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

CommandLine   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 134
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A redirectOutputTo() 0 4 1
A isOutputRedirected() 0 4 1
A getRedirectPath() 0 4 1
A pipeOutputTo() 0 7 2
A canPipe() 0 4 1
A isPiped() 0 4 1
A getPipeline() 0 4 2
A addCommand() 0 4 1
A getCommand() 0 12 4
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;
11
12
use RuntimeException;
13
14
/**
15
 * Class CommandLine
16
 *
17
 * @package SebastianFeldmann\Cli
18
 */
19
class CommandLine implements Command
20
{
21
    /**
22
     * List of system commands to execute.
23
     *
24
     * @var \SebastianFeldmann\Cli\Command[]
25
     */
26
    private $commands = [];
27
28
    /**
29
     * Redirect the output
30
     *
31
     * @var string
32
     */
33
    private $redirectOutput;
34
35
    /**
36
     * Output pipeline
37
     *
38
     * @var \SebastianFeldmann\Cli\Command[]
39
     */
40
    private $pipeline = [];
41
42
    /**
43
     * Redirect the stdOut.
44
     *
45
     * @param string $path
46
     */
47 1
    public function redirectOutputTo($path)
48
    {
49 1
        $this->redirectOutput = $path;
50 1
    }
51
52
    /**
53
     * Should the output be redirected.
54
     *
55
     * @return boolean
56
     */
57 1
    public function isOutputRedirected()
58
    {
59 1
        return !empty($this->redirectOutput);
60
    }
61
62
    /**
63
     * Redirect getter.
64
     *
65
     * @return string
66
     */
67 1
    public function getRedirectPath()
68
    {
69 1
        return $this->redirectOutput;
70
    }
71
72
    /**
73
     * Pipe the command into given command.
74
     *
75
     * @param \SebastianFeldmann\Cli\Command $cmd
76
     */
77 1
    public function pipeOutputTo(Command $cmd)
78
    {
79 1
        if (!$this->canPipe()) {
80
            throw new RuntimeException('Can\'t pipe output');
81
        }
82 1
        $this->pipeline[] = $cmd;
83 1
    }
84
85
    /**
86
     * Can the pipe '|' operator be used.
87
     *
88
     * @return bool
89
     */
90 1
    public function canPipe()
91
    {
92 1
        return !defined('PHP_WINDOWS_VERSION_BUILD');
93
    }
94
95
    /**
96
     * Is there a command pipeline.
97
     *
98
     * @return bool
99
     */
100 3
    public function isPiped()
101
    {
102 3
        return !empty($this->pipeline);
103
    }
104
105
    /**
106
     * Return command pipeline.
107
     *
108
     * @return string
109
     */
110 3
    public function getPipeline()
111
    {
112 3
        return $this->isPiped() ? ' | ' . implode(' | ', $this->pipeline) : '';
113
    }
114
115
    /**
116
     * Adds a cli command to list of commands to execute.
117
     *
118
     * @param \SebastianFeldmann\Cli\Command
119
     */
120 4
    public function addCommand(Command $cmd)
121
    {
122 4
        $this->commands[] = $cmd;
123 4
    }
124
125
    /**
126
     * Generates the system command.
127
     *
128
     * @return string
129
     */
130 4
    public function getCommand() : string
131
    {
132 4
        $amount = count($this->commands);
133 4
        if ($amount < 1) {
134 1
            throw new RuntimeException('no command to execute');
135
        }
136 3
        $cmd = ($amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0])
137 3
             . $this->getPipeline()
138 3
             . (!empty($this->redirectOutput) ? ' > ' . $this->redirectOutput : '');
139
140 3
        return $cmd;
141
    }
142
143
    /**
144
     * Returns the command to execute.
145
     *
146
     * @return string
147
     */
148 1
    public function __toString() : string
149
    {
150 1
        return $this->getCommand();
151
    }
152
}
153