Completed
Push — master ( 482c5f...79d98c )
by Jan Philipp
13s queued 11s
created

ConfigBuilder::setDotenvPath()   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
/**
6
 * Builder pattern
7
 *
8
 * Create a config from a more complicated proto format by representing a stateful representation of config read so far.
9
 */
10
class ConfigBuilder
11
{
12
    const DEFAULT_ENV = '##default##';
13
14
    private $header = '';
15
16
    private $environments = [];
17
18
    private $currentEnvironment;
19
20
    private $currentCommandPaths;
21
22
    private $currentDotenvPaths;
23
24
    private $currentDynamicVariables;
25
26
    private $templates;
27
28
    private $currentConstants;
29
30
    private $hidden;
31
32
    /**
33
     * @param string|null $header
34
     * @return ConfigBuilder
35
     */
36
    public function setHeader(string $header = null): ConfigBuilder
37
    {
38
        $this->header = $header;
39
        return $this;
40
    }
41
42
    /**
43
     * @param string|null $environment
44
     * @return ConfigBuilder
45
     */
46
    public function start(string $environment = null): ConfigBuilder
47
    {
48
        $this->reset();
49
        if ($environment === null) {
50
            $environment = self::DEFAULT_ENV;
51
        }
52
53
        $this->currentEnvironment = $environment;
54
        return $this;
55
    }
56
57
    /**
58
     * @param bool $set
59
     * @return ConfigBuilder
60
     */
61
    public function setHidden(bool $set): ConfigBuilder
62
    {
63
        $this->hidden = $set;
64
        return $this;
65
    }
66
67
    /**
68
     * @param array $commandPaths
69
     * @return ConfigBuilder
70
     */
71
    public function setCommandPaths(array $commandPaths): ConfigBuilder
72
    {
73
        $this->currentCommandPaths = $commandPaths;
74
        return $this;
75
    }
76
77
    /**
78
     * @param array $dotenvPaths
79
     * @return ConfigBuilder
80
     */
81
    public function setDotenvPaths(array $dotenvPaths): ConfigBuilder
82
    {
83
        $this->currentDotenvPaths = [];
84
85
        foreach ($dotenvPaths as $dotenvPath) {
86
            $this->setDotenvPath($dotenvPath);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param string $dotenvPath
94
     * @return ConfigBuilder
95
     */
96
    public function setDotenvPath(string $dotenvPath): ConfigBuilder
97
    {
98
        $this->currentDotenvPaths[pathinfo($dotenvPath, PATHINFO_BASENAME)] = $dotenvPath;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @deprecated only used by yaml builder
105
     * @param array $dynamicVariables
106
     * @return ConfigBuilder
107
     */
108
    public function setDynamicVariables(array $dynamicVariables): ConfigBuilder
109
    {
110
        $this->currentDynamicVariables = $dynamicVariables;
111
        return $this;
112
    }
113
114
    public function setDynamicVariable(string $key, string $value): ConfigBuilder
115
    {
116
        $this->currentDynamicVariables[$key] = $value;
117
        return $this;
118
    }
119
120
    /**
121
     * @deprecated only used by yaml builder
122
     * @param array $constants
123
     * @return ConfigBuilder
124
     */
125
    public function setConstants(array $constants): ConfigBuilder
126
    {
127
        $this->currentConstants = $constants;
128
        return $this;
129
    }
130
131
    public function setConstVariable(string $key, string $value): ConfigBuilder
132
    {
133
        $this->currentConstants[$key] = $value;
134
        return $this;
135
    }
136
137
    /**
138
     * @param array $templates
139
     * @return ConfigBuilder
140
     */
141
    public function setTemplates(array $templates): ConfigBuilder
142
    {
143
        $this->templates = $templates;
144
        return $this;
145
    }
146
147
    /**
148
     * @return Config
149
     */
150
    public function create(array $params): Config
151
    {
152
        $this->reset();
153
154
        return new Config($this->header, self::DEFAULT_ENV, $this->environments, $params);
155
    }
156
157
    private function reset()
158
    {
159
        if ($this->currentEnvironment) {
160
            $this->environments[$this->currentEnvironment] = new ConfigEnvironment(
161
                $this->hidden,
162
                $this->currentCommandPaths,
163
                $this->currentDynamicVariables,
164
                $this->currentConstants,
165
                $this->templates,
166
                $this->currentDotenvPaths
167
            );
168
        }
169
170
        $this->currentCommandPaths = [];
171
        $this->currentDotenvPaths = [];
172
        $this->currentDynamicVariables = [];
173
        $this->currentConstants = [];
174
        $this->templates = [];
175
        $this->hidden = false;
176
    }
177
}
178