Completed
Push — master ( 0a4926...d192d3 )
by Jan Philipp
24s queued 11s
created

CommandBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 142
rs 10
c 0
b 0
f 0

10 Methods

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