Passed
Push — master ( 7c2abd...e1c388 )
by
unknown
03:51 queued 14s
created

ShellBuilder::redirectErrorToOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPSu\ShellCommandBuilder;
6
7
use PHPSu\ShellCommandBuilder\Collection\CollectionInterface;
8
use PHPSu\ShellCommandBuilder\Collection\CollectionTuple;
9
use PHPSu\ShellCommandBuilder\Collection\Pipeline;
10
use PHPSu\ShellCommandBuilder\Collection\Redirection;
11
use PHPSu\ShellCommandBuilder\Collection\ShellList;
12
use PHPSu\ShellCommandBuilder\Definition\ControlOperator;
13
use PHPSu\ShellCommandBuilder\Definition\GroupType;
14
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
15
16
final class ShellBuilder implements ShellInterface
17
{
18
    /** @var array<ShellInterface|CollectionTuple|CollectionInterface>  */
19
    private $commandList = [];
20
    /** @var int */
21
    private $groupType;
22
23
    public function __construct(int $groupType = GroupType::NO_GROUP)
24
    {
25
        $this->groupType = $groupType;
26
    }
27
28
    public function createCommand(string $name, bool $withNewBuilder = false): ShellCommand
29
    {
30
        return new ShellCommand($name, $withNewBuilder ? new self() : $this);
31
    }
32
33
    /**
34
     * @param string|ShellInterface $command
35
     * @return $this
36
     * @throws ShellBuilderException
37
     */
38
    public function add($command): self
39
    {
40
        $command = $this->parseCommand($command, true);
41
        if (empty($this->commandList)) {
42
            $this->commandList[] = $command;
43
            return $this;
44
        }
45
        $list = new ShellList();
46
        $list->add($command);
47
        $this->commandList[] = $list;
48
        return $this;
49
    }
50
51
    /**
52
     * @param string|ShellInterface $command
53
     * @return $this
54
     * @throws ShellBuilderException
55
     */
56
    public function and($command): self
57
    {
58
        $list = new ShellList();
59
        $list->addAnd($this->parseCommand($command));
60
        $this->commandList[] = $list;
61
        return $this;
62
    }
63
64
    /**
65
     * @param string|ShellInterface $command
66
     * @return $this
67
     * @throws ShellBuilderException
68
     */
69
    public function or($command): self
70
    {
71
        $list = new ShellList();
72
        $list->addOr($this->parseCommand($command));
73
        $this->commandList[] = $list;
74
        return $this;
75
    }
76
77
    /**
78
     * @param string|ShellInterface $command
79
     * @return $this
80
     * @throws ShellBuilderException
81
     */
82
    public function pipe($command): self
83
    {
84
        $list = new Pipeline();
85
        $list->pipe($this->parseCommand($command));
86
        $this->commandList[] = $list;
87
        return $this;
88
    }
89
90
    /**
91
     * @param string|ShellInterface $command
92
     * @return $this
93
     * @throws ShellBuilderException
94
     */
95
    public function pipeWithForward($command): self
96
    {
97
        $list = new Pipeline();
98
        $list->pipeErrorForward($this->parseCommand($command));
99
        $this->commandList[] = $list;
100
        return $this;
101
    }
102
103
    /**
104
     * @param string|ShellInterface $command
105
     * @param bool $append
106
     * @return $this
107
     * @throws ShellBuilderException
108
     */
109
    public function redirectOutput($command, bool $append = false): self
110
    {
111
        $redirect = new Redirection();
112
        $command = $this->parseCommand($command);
113
        $this->commandList[] = $redirect->redirectOutput($command, $append);
114
        return $this;
115
    }
116
117
    /**
118
     * @param string|ShellInterface $command
119
     * @return $this
120
     * @throws ShellBuilderException
121
     */
122
    public function redirectInput($command): self
123
    {
124
        $redirect = new Redirection();
125
        $command = $this->parseCommand($command);
126
        $this->commandList[] = $redirect->redirectInput($command);
127
        return $this;
128
    }
129
130
    /**
131
     * @param string|ShellInterface $command
132
     * @return $this
133
     * @throws ShellBuilderException
134
     */
135
    public function redirectError($command): self
136
    {
137
        $redirect = new Redirection();
138
        $command = $this->parseCommand($command);
139
        $this->commandList[] = $redirect->redirectError($command);
140
        return $this;
141
    }
142
143
    /**
144
     * @param string|ShellInterface $command
145
     * @param bool $toLeft
146
     * @return $this
147
     * @throws ShellBuilderException
148
     */
149
    public function redirect($command, bool $toLeft = true): self
150
    {
151
        $redirect = new Redirection();
152
        $command = $this->parseCommand($command);
153
        $this->commandList[] = $redirect->redirectBetweenFiles($command, $toLeft);
154
        return $this;
155
    }
156
157
    public function redirectErrorToOutput(): self
158
    {
159
        $this->commandList[] = (new Redirection())->redirectErrorToOutput();
160
        return $this;
161
    }
162
163
    public function createGroup(bool $inSameShell = false): self
164
    {
165
        return new self($inSameShell ? GroupType::SAMESHELL_GROUP : GroupType::SUBSHELL_GROUP);
166
    }
167
168
    /**
169
     * @param string|ShellInterface $command
170
     * @param bool $allowEmpty
171
     * @return ShellInterface
172
     * @throws ShellBuilderException
173
     */
174
    private function parseCommand($command, bool $allowEmpty = false): ShellInterface
175
    {
176
        if (is_string($command)) {
177
            $command = $this->createCommand($command);
178
        }
179
        try {
180
            $this->validateCommand($command, $allowEmpty);
181
        } catch (\TypeError $typeError) {
182
            throw new ShellBuilderException('Provided the wrong type - only ShellCommand and ShellBuilder allowed');
183
        }
184
        return $command;
185
    }
186
187
    private function validateCommand(ShellInterface $command, bool $allowEmpty): void
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

187
    private function validateCommand(/** @scrutinizer ignore-unused */ ShellInterface $command, bool $allowEmpty): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
    {
189
        if (!$allowEmpty && empty($this->commandList)) {
190
            throw new ShellBuilderException('You have to first add a command before you can combine it');
191
        }
192
    }
193
194
    /**
195
     * @return array<mixed>
196
     */
197
    public function __toArray(): array
198
    {
199
        $commands = [];
200
        foreach ($this->commandList as $item) {
201
            $commands[] = $item instanceof ShellInterface ? $item->__toArray() : $item;
202
        }
203
        return $commands;
204
    }
205
206
    public function __toString(): string
207
    {
208
        $result = '';
209
        foreach ($this->commandList as $command) {
210
            $result .= $command;
211
        }
212
        if ($this->groupType === GroupType::SAMESHELL_GROUP) {
213
            return sprintf(
214
                '%s%s;%s',
215
                ControlOperator::CURLY_BLOCK_DEFINITON_OPEN,
216
                $result,
217
                ControlOperator::CURLY_BLOCK_DEFINITON_CLOSE
218
            );
219
        }
220
        if ($this->groupType === GroupType::SUBSHELL_GROUP) {
221
            return sprintf(
222
                '%s%s%s',
223
                ControlOperator::BLOCK_DEFINITON_OPEN,
224
                $result,
225
                ControlOperator::BLOCK_DEFINITON_CLOSE
226
            );
227
        }
228
        return $result;
229
    }
230
}
231