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

Config::getDotenvPaths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
/**
6
 * Represents the global configuration consisting of multiple environments
7
 */
8
class Config
9
{
10
    /**
11
     * @var string
12
     */
13
    private $header;
14
15
    /**
16
     * @var string
17
     */
18
    private $defaultEnvironment;
19
20
    /**
21
     * @var ConfigEnvironment[]
22
     */
23
    private $environments;
24
25
    /**
26
     * @var array
27
     */
28
    private $params;
29
30
    /**
31
     * @param string|null $header
32
     * @param string $defaultEnvironment
33
     * @param ConfigEnvironment[] $environments
34
     * @param array $params
35
     */
36
    public function __construct(
37
        string $header = null,
38
        string $defaultEnvironment,
39
        array $environments,
40
        array $params
41
    ) {
42
        $this->header = $header;
43
        $this->defaultEnvironment = $defaultEnvironment;
44
        $this->environments = $environments;
45
        $this->params = $params;
46
    }
47
48
    /**
49
     * @return ScriptPath[]
50
     */
51
    public function getAllScriptPaths(): array
52
    {
53
        $paths = [];
54
55 View Code Duplication
        foreach ($this->environments as $name => $environmentConfig) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            foreach ($environmentConfig->getAllScriptPaths() as $path) {
57
                if ($name !== $this->defaultEnvironment) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $name (integer) and $this->defaultEnvironment (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
58
                    $paths[] = new ScriptPath($path, $name);
59
                } else {
60
                    $paths[] = new ScriptPath($path);
61
                }
62
            }
63
        }
64
65
        return $paths;
66
    }
67
68
    /**
69
     * @param string|null $environment
70
     * @return array
71
     */
72
    public function getTemplates(string $environment = null): array
73
    {
74
        return $this->createResult(
75
            [$this->getEnvironment(), 'getTemplates'],
76
            [$this->getEnvironment($environment), 'getTemplates']
77
        );
78
    }
79
80
    /**
81
     * @param string|null $environment
82
     * @return array
83
     */
84
    public function getDynamicVariables(string $environment = null): array
85
    {
86
        return $this->createResult(
87
            [$this->getEnvironment(), 'getDynamicVariables'],
88
            [$this->getEnvironment($environment), 'getDynamicVariables']
89
        );
90
    }
91
92
    /**
93
     * @param string|null $environment
94
     * @return array
95
     */
96
    public function getConstants(string $environment = null): array
97
    {
98
        return $this->createResult(
99
            [$this->getEnvironment(), 'getConstants'],
100
            [$this->getEnvironment($environment), 'getConstants'],
101
            [$this, 'getParams']
102
        );
103
    }
104
105
    /**
106
     * @param string|null $environmentString
107
     *
108
     * @return ScriptPath[]
109
     */
110
    public function getDotenvPaths(string $environmentString = null): array
111
    {
112
        $environmentString = $environmentString ?? ConfigBuilder::DEFAULT_ENV;
113
114
        $paths = $this->resolveEnvironmentDotEnvPaths(
115
            $this->getEnvironment($this->defaultEnvironment),
116
            $this->defaultEnvironment
117
        );
118
119
        if ($environmentString !== $this->defaultEnvironment) {
120
            $environmentPaths = $this->resolveEnvironmentDotEnvPaths(
121
                $this->getEnvironment($environmentString),
122
                $environmentString
123
            );
124
125
            if ($environmentPaths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $environmentPaths of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
126
                $paths = $environmentPaths;
127
            }
128
        }
129
130
        return $paths;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getHeader()
137
    {
138
        return $this->header;
139
    }
140
141
    /**
142
     * @return ConfigEnvironment[]
143
     */
144
    public function getEnvironments(): array
145
    {
146
        return $this->environments;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getDefaultEnvironment(): string
153
    {
154
        return $this->defaultEnvironment;
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function getParams() : array
161
    {
162
        return $this->params;
163
    }
164
165
    /**
166
     * @param callable[] ...$valueProviders
167
     * @return array
168
     */
169
    private function createResult(callable ...$valueProviders): array
170
    {
171
        $mergedKeyValues = [];
172
173
        foreach ($valueProviders as $valueProvider) {
174
            foreach ($valueProvider() as $key => $value) {
175
                $mergedKeyValues[$key] = $value;
176
            }
177
        }
178
179
        return $mergedKeyValues;
180
    }
181
182
    /**
183
     * @param string|null $name
184
     * @return ConfigEnvironment
185
     */
186
    private function getEnvironment(string $name = null): ConfigEnvironment
187
    {
188
        if (!$name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name 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...
189
            return $this->environments[$this->defaultEnvironment];
190
        }
191
192
        return $this->environments[$name];
193
    }
194
195
    /**
196
     * @param ConfigEnvironment $environmentConfig
197
     * @param string|null $name
198
     *
199
     * @return array
200
     */
201
    private function resolveEnvironmentDotEnvPaths(ConfigEnvironment $environmentConfig, string $name = null): array
202
    {
203
        $paths = [];
204
205 View Code Duplication
        foreach ($environmentConfig->getDotenvPaths() as $path) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
            if ($name !== $this->defaultEnvironment) {
207
                $paths[] = new ScriptPath($path, $name);
208
            } else {
209
                $paths[] = new ScriptPath($path);
210
            }
211
        }
212
213
        return $paths;
214
    }
215
}
216