Completed
Push — master ( 1ece6c...656998 )
by Jan Philipp
13s queued 11s
created

CommandBuilder::addTemplateCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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