Completed
Pull Request — master (#80)
by Jan Philipp
01:36
created

CommandBuilder::addWaitCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\ScriptRuntime;
5
6
/**
7
 * Again, a statefull builder to ease the parsing
8
 */
9
class CommandBuilder
10
{
11
    /**
12
     * @var ProcessCommand[]
13
     */
14
    private $allCommands = [];
15
16
    /**
17
     * @var string
18
     */
19
    private $currentShellCommand;
20
21
    /**
22
     * @var int
23
     */
24
    private $startLine;
25
26
    /**
27
     * @var bool
28
     */
29
    private $ignoreError;
30
31
    /**
32
     * @var bool
33
     */
34
    private $tty;
35
36
    /**
37
     * @var bool
38
     */
39
    private $template;
40
41
    /**
42
     * @var bool
43
     */
44
    private $deferred;
45
46
    private function reset()
47
    {
48
        if ($this->currentShellCommand) {
49
            $this->allCommands[] = new ProcessCommand(
50
                $this->currentShellCommand,
51
                $this->startLine,
52
                $this->ignoreError,
53
                $this->tty,
54
                $this->deferred
55
            );
56
        }
57
58
        $this->currentShellCommand = null;
59
        $this->startLine = null;
60
        $this->ignoreError = false;
61
        $this->tty = false;
62
        $this->template = false;
63
        $this->deferred = false;
64
    }
65
66
    /**
67
     * @param string $shellCommand
68
     * @param int $startLine
69
     * @param bool $ignoreError
70
     * @param bool $tty
71
     * @param bool $deferred
72
     * @return CommandBuilder
73
     */
74
    public function next(string $shellCommand, int $startLine, bool $ignoreError, bool $tty, bool $deferred): CommandBuilder
75
    {
76
        $this->reset();
77
78
        $this->currentShellCommand = $shellCommand;
79
        $this->startLine = $startLine;
80
        $this->ignoreError = $ignoreError;
81
        $this->tty = $tty;
82
        $this->deferred = $deferred;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param string $source
89
     * @param string $destination
90
     * @param int $lineNumber
91
     * @return $this
92
     */
93
    public function addTemplateCommand(string $source, string $destination, int $lineNumber)
94
    {
95
        $this->reset();
96
97
        $this->allCommands[] = new TemplateCommand(
98
            $source,
99
            $destination,
100
            $lineNumber
101
        );
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param int $lineNumber
108
     * @return CommandBuilder
109
     */
110
    public function addWaitCommand(int $lineNumber): CommandBuilder
111
    {
112
        $this->reset();
113
        $this->allCommands[] = new WaitCommand($lineNumber);
114
115
        return $this;
116
    }
117
118
    /**
119
     * @param bool $set
120
     * @return CommandBuilder
121
     */
122
    public function setIgnoreError(bool $set = true): CommandBuilder
123
    {
124
        $this->ignoreError = $set;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param string $shellCommand
131
     * @return CommandBuilder
132
     */
133
    public function add(string $shellCommand): CommandBuilder
134
    {
135
        $this->currentShellCommand .= ' ' . trim($shellCommand);
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param array $commands
142
     * @return CommandBuilder
143
     */
144
    public function setCommands(array $commands): CommandBuilder
145
    {
146
        $this->allCommands = $commands;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return ProcessCommand[]
153
     */
154
    public function getAll(): array
155
    {
156
        $this->reset();
157
        $allCommands = $this->allCommands;
158
        $this->allCommands = [];
159
160
        return $allCommands;
161
    }
162
}
163