Completed
Push — master ( be53ba...d8ac27 )
by Jan Philipp
11s
created

CommandBuilder::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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