Completed
Pull Request — master (#86)
by Jan Philipp
01:42
created

ConfigBuilder::setDotenvPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
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 = [];
72
73
        foreach ($dotenvPaths as $dotenvPath) {
74
            $this->currentDotenvPaths[pathinfo($dotenvPath, PATHINFO_BASENAME)] = $dotenvPath;
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param array $dynamicVariables
82
     * @return ConfigBuilder
83
     */
84
    public function setDynamicVariables(array $dynamicVariables): ConfigBuilder
85
    {
86
        $this->currentDynamicVariables = $dynamicVariables;
87
        return $this;
88
    }
89
90
    /**
91
     * @param array $constants
92
     * @return ConfigBuilder
93
     */
94
    public function setConstants(array $constants): ConfigBuilder
95
    {
96
        $this->currentConstants = $constants;
97
        return $this;
98
    }
99
100
    /**
101
     * @param array $templates
102
     * @return ConfigBuilder
103
     */
104
    public function setTemplates(array $templates): ConfigBuilder
105
    {
106
        $this->templates = $templates;
107
        return $this;
108
    }
109
110
    /**
111
     * @return Config
112
     */
113
    public function create(array $params): Config
114
    {
115
        $this->reset();
116
117
        return new Config($this->header, self::DEFAULT_ENV, $this->environments, $params);
118
    }
119
120
    private function reset()
121
    {
122
        if ($this->currentEnvironment) {
123
            $this->environments[$this->currentEnvironment] = new ConfigEnvironment(
124
                $this->currentCommandPaths,
125
                $this->currentDynamicVariables,
126
                $this->currentConstants,
127
                $this->templates,
128
                $this->currentDotenvPaths
129
            );
130
        }
131
132
        $this->currentCommandPaths = [];
133
        $this->currentDotenvPaths = [];
134
        $this->currentDynamicVariables = [];
135
        $this->currentConstants = [];
136
        $this->templates = [];
137
    }
138
}
139