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

Config::getHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
            $paths = $this->resolveEnvironmentDotEnvPaths(
121
                $this->getEnvironment($environmentString),
122
                $environmentString
123
            );
124
        }
125
126
        return $paths;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getHeader()
133
    {
134
        return $this->header;
135
    }
136
137
    /**
138
     * @return ConfigEnvironment[]
139
     */
140
    public function getEnvironments(): array
141
    {
142
        return $this->environments;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getDefaultEnvironment(): string
149
    {
150
        return $this->defaultEnvironment;
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function getParams() : array
157
    {
158
        return $this->params;
159
    }
160
161
    /**
162
     * @param callable[] ...$valueProviders
163
     * @return array
164
     */
165
    private function createResult(callable ...$valueProviders): array
166
    {
167
        $mergedKeyValues = [];
168
169
        foreach ($valueProviders as $valueProvider) {
170
            foreach ($valueProvider() as $key => $value) {
171
                $mergedKeyValues[$key] = $value;
172
            }
173
        }
174
175
        return $mergedKeyValues;
176
    }
177
178
    /**
179
     * @param string|null $name
180
     * @return ConfigEnvironment
181
     */
182
    private function getEnvironment(string $name = null): ConfigEnvironment
183
    {
184
        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...
185
            return $this->environments[$this->defaultEnvironment];
186
        }
187
188
        return $this->environments[$name];
189
    }
190
191
    /**
192
     * @param ConfigEnvironment $environmentConfig
193
     * @param string|null $name
194
     *
195
     * @return array
196
     */
197
    private function resolveEnvironmentDotEnvPaths(ConfigEnvironment $environmentConfig, string $name = null): array
198
    {
199
        $paths = [];
200
201 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...
202
            if ($name !== $this->defaultEnvironment) {
203
                $paths[] = new ScriptPath($path, $name);
204
            } else {
205
                $paths[] = new ScriptPath($path);
206
            }
207
        }
208
209
        return $paths;
210
}
211
}
212