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

CommandBuilder::addProcessCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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