Completed
Pull Request — master (#111)
by Jan Philipp
01:50
created

ConfigBuilder::setWorkingDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
use function pathinfo;
6
7
/**
8
 * Builder pattern
9
 *
10
 * Create a config from a more complicated proto format by representing a stateful representation of config read so far.
11
 */
12
class ConfigBuilder
13
{
14
    const DEFAULT_ENV = '##default##';
15
16
    private $header = '';
17
18
    private $environments = [];
19
20
    /**
21
     * @var string|null
22
     */
23
    private $currentEnvironment;
24
25
    private $currentCommandPaths;
26
27
    private $currentDotenvPaths;
28
29
    private $currentDynamicVariables;
30
31
    private $templates;
32
33
    private $currentConstants;
34
35
    private $hidden;
36
37
    private $currentRequiredVariables;
38
39
    private $imports;
40
41
    /**
42
     * @var string
43
     */
44
    private $workingDirectory;
45
46
    public function setHeader(?string $header = null): ConfigBuilder
47
    {
48
        $this->header = $header;
49
50
        return $this;
51
    }
52
53
    public function setWorkingDirectory(string $workingDirectory): ConfigBuilder
54
    {
55
        $this->workingDirectory = $workingDirectory;
56
57
        return $this;
58
    }
59
60
    public function start(?string $environment = null): ConfigBuilder
61
    {
62
        $this->reset();
63
        if ($environment === null) {
64
            $environment = self::DEFAULT_ENV;
65
        }
66
67
        $this->currentEnvironment = $environment;
68
69
        return $this;
70
    }
71
72
    public function setHidden(bool $set): ConfigBuilder
73
    {
74
        $this->hidden = $set;
75
76
        return $this;
77
    }
78
79
    public function setCommandPaths(array $commandPaths): ConfigBuilder
80
    {
81
        $this->currentCommandPaths = [];
82
83
        foreach ($commandPaths as $path) {
84
            $env = $this->currentEnvironment;
85
86
            if ($env === self::DEFAULT_ENV) {
87
                $env = null;
88
            }
89
90
            $this->currentCommandPaths[] = new ScriptsPath($path, $this->workingDirectory, $this->hidden, $env);
91
        }
92
93
        return $this;
94
    }
95
96
    /**
97
     * @deprecated only used by yaml builder
98
     */
99
    public function setDotenvPaths(array $dotenvPaths): ConfigBuilder
100
    {
101
        $this->currentDotenvPaths = [];
102
103
        foreach ($dotenvPaths as $dotenvPath) {
104
            $this->addDotenvPath($dotenvPath);
105
        }
106
107
        return $this;
108
    }
109
110
    public function addDotenvPath(string $dotenvPath): ConfigBuilder
111
    {
112
        $this->currentDotenvPaths[pathinfo($dotenvPath, PATHINFO_BASENAME)] = $dotenvPath;
113
114
        return $this;
115
    }
116
117
    public function addRequirePlaceholder(string $name, ?string $description = null): ConfigBuilder
118
    {
119
        $this->currentRequiredVariables[$name] = $description;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @deprecated only used by yaml builder
126
     */
127
    public function setDynamicVariables(array $dynamicVariables): ConfigBuilder
128
    {
129
        $this->currentDynamicVariables = $dynamicVariables;
130
131
        return $this;
132
    }
133
134
    public function addDynamicVariable(string $key, string $value): ConfigBuilder
135
    {
136
        $this->currentDynamicVariables[$key] = $value;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @deprecated only used by yaml builder
143
     */
144
    public function setConstants(array $constants): ConfigBuilder
145
    {
146
        $this->currentConstants = $constants;
147
148
        return $this;
149
    }
150
151
    public function addConstVariable(string $key, string $value): ConfigBuilder
152
    {
153
        $this->currentConstants[$key] = $value;
154
155
        return $this;
156
    }
157
158
    public function addImport(string $path): self
159
    {
160
        $this->imports[] = $path;
161
162
        return $this;
163
    }
164
165
    public function addTemplate(string $source, string $destination, string $baseFile): ConfigBuilder
166
    {
167
        $this->templates[] = new Template($source, $destination, $baseFile);
168
169
        return $this;
170
    }
171
172
    public function setTemplates(array $templates, string $baseFile): ConfigBuilder
173
    {
174
        $this->templates = [];
175
176
        foreach ($templates as $template) {
177
            $this->addTemplate($template['source'], $template['destination'], $baseFile);
178
        }
179
180
        return $this;
181
    }
182
183
    public function create(array $params): Config
184
    {
185
        $this->reset();
186
187
        return new Config(new EnvironmentResolver(), self::DEFAULT_ENV, $this->environments, $params, $this->header);
188
    }
189
190
    private function reset(): void
191
    {
192
        if ($this->currentEnvironment) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->currentEnvironment of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
193
            $this->environments[$this->currentEnvironment] = new ConfigEnvironment(
194
                $this->hidden,
195
                $this->currentCommandPaths,
196
                $this->currentDynamicVariables,
197
                $this->currentConstants,
198
                $this->templates,
199
                $this->currentDotenvPaths,
200
                $this->currentRequiredVariables,
201
                $this->imports
202
            );
203
        }
204
205
        $this->currentCommandPaths = [];
206
        $this->currentDotenvPaths = [];
207
        $this->currentDynamicVariables = [];
208
        $this->currentConstants = [];
209
        $this->templates = [];
210
        $this->hidden = false;
211
        $this->currentRequiredVariables = [];
212
        $this->imports = [];
213
    }
214
}
215