Completed
Pull Request — master (#84)
by Nick
01:52
created

ConfigBuilder::setDynamicVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
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
    /**
31
     * @param string|null $header
32
     * @return ConfigBuilder
33
     */
34
    public function setHeader(string $header = null): ConfigBuilder
35
    {
36
        $this->header = $header;
37
        return $this;
38
    }
39
40
    /**
41
     * @param string|null $environment
42
     * @return ConfigBuilder
43
     */
44
    public function start(string $environment = null): ConfigBuilder
45
    {
46
        $this->reset();
47
        if (!$environment) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $environment of type null|string is loosely compared to false; 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...
48
            $environment = self::DEFAULT_ENV;
49
        }
50
51
        $this->currentEnvironment = $environment;
52
        return $this;
53
    }
54
55
    /**
56
     * @param array $commandPaths
57
     * @return ConfigBuilder
58
     */
59
    public function setCommandPaths(array $commandPaths): ConfigBuilder
60
    {
61
        $this->currentCommandPaths = $commandPaths;
62
        return $this;
63
    }
64
65
    /**
66
     * @param array $dotenvPaths
67
     * @return ConfigBuilder
68
     */
69
    public function setDotenvPaths(array $dotenvPaths): ConfigBuilder
70
    {
71
        $this->currentDotenvPaths = $dotenvPaths;
72
        return $this;
73
    }
74
75
    /**
76
     * @param array $dynamicVariables
77
     * @return ConfigBuilder
78
     */
79
    public function setDynamicVariables(array $dynamicVariables): ConfigBuilder
80
    {
81
        $this->currentDynamicVariables = $dynamicVariables;
82
        return $this;
83
    }
84
85
    /**
86
     * @param array $constants
87
     * @return ConfigBuilder
88
     */
89
    public function setConstants(array $constants): ConfigBuilder
90
    {
91
        $this->currentConstants = $constants;
92
        return $this;
93
    }
94
95
    /**
96
     * @param array $templates
97
     * @return ConfigBuilder
98
     */
99
    public function setTemplates(array $templates): ConfigBuilder
100
    {
101
        $this->templates = $templates;
102
        return $this;
103
    }
104
105
    /**
106
     * @return Config
107
     */
108
    public function create(array $params): Config
109
    {
110
        $this->reset();
111
112
        return new Config($this->header, self::DEFAULT_ENV, $this->environments, $params);
113
    }
114
115
    private function reset()
116
    {
117
        if ($this->currentEnvironment) {
118
            $this->environments[$this->currentEnvironment] = new ConfigEnvironment(
119
                $this->currentCommandPaths,
120
                $this->currentDynamicVariables,
121
                $this->currentConstants,
122
                $this->templates,
123
                $this->currentDotenvPaths
124
            );
125
        }
126
127
        $this->currentCommandPaths = [];
128
        $this->currentDotenvPaths = [];
129
        $this->currentDynamicVariables = [];
130
        $this->currentConstants = [];
131
        $this->templates = [];
132
    }
133
}
134