Completed
Pull Request — master (#111)
by Jan Philipp
02:10
created

Config::createUpperCaseResult()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
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
use Shopware\Psh\Application\RuntimeParameters;
6
use function array_map;
7
use function array_merge;
8
use function mb_strtoupper;
9
10
/**
11
 * Represents the global configuration consisting of multiple environments
12
 */
13
class Config
14
{
15
    /**
16
     * @var EnvironmentResolver
17
     */
18
    private $resolver;
19
20
    /**
21
     * @var string
22
     */
23
    private $header;
24
25
    /**
26
     * @var string
27
     */
28
    private $defaultEnvironment;
29
30
    /**
31
     * @var ConfigEnvironment[]
32
     */
33
    private $environments;
34
35
    /**
36
     * @var RuntimeParameters
37
     */
38
    private $runtimeParameters;
39
40
    /**
41
     * @param ConfigEnvironment[] $environments
42
     */
43
    public function __construct(
44
        EnvironmentResolver $resolver,
45
        string $defaultEnvironment,
46
        array $environments,
47
        RuntimeParameters $runtimeParameters,
48
        ?string $header = null
49
    ) {
50
        $this->resolver = $resolver;
51
        $this->defaultEnvironment = $defaultEnvironment;
52
        $this->environments = $environments;
53
        $this->runtimeParameters = $runtimeParameters;
54
        $this->header = $header;
55
    }
56
57
    /**
58
     * @return ScriptsPath[]
59
     */
60
    public function getAllScriptsPaths(): array
61
    {
62
        $paths = [];
63
64
        foreach ($this->environments as $name => $environmentConfig) {
65
            foreach ($environmentConfig->getAllScriptsPaths() as $path) {
66
                $paths[] = $path;
67
            }
68
        }
69
70
        return $paths;
71
    }
72
73
    public function getTemplates(?string $environment = null): array
74
    {
75
        return $this->createResult(
76
            [$this->getEnvironment(), 'getTemplates'],
77
            [$this->getEnvironment($environment), 'getTemplates']
78
        );
79
    }
80
81
    public function getDynamicVariables(?string $environment = null): array
82
    {
83
        return $this->resolver->resolveVariables($this->createUpperCaseResult(
84
            [$this->getEnvironment(), 'getDynamicVariables'],
85
            [$this->getEnvironment($environment), 'getDynamicVariables']
86
        ));
87
    }
88
89
    public function getConstants(?string $environment = null): array
90
    {
91
        return $this->resolver->resolveConstants($this->createUpperCaseResult(
92
            [$this->getEnvironment(), 'getConstants'],
93
            [$this->getEnvironment($environment), 'getConstants'],
94
            [$this, 'getParams']
95
        ));
96
    }
97
98
    public function getAllPlaceholders(?string $environment = null): array
99
    {
100
        return array_merge(
101
            $this->getConstants($environment),
102
            $this->getDotenvVariables($environment),
103
            $this->getDynamicVariables($environment)
104
        );
105
    }
106
107
    /**
108
     * @return ValueProvider[]
109
     */
110
    public function getDotenvVariables(?string $environment = null): array
111
    {
112
        $paths = $this->getDotenvPaths($environment);
113
114
        return $this->resolver->resolveDotenvVariables($paths);
115
    }
116
117
    /**
118
     * @return RequiredValue[]
119
     */
120
    public function getRequiredVariables(?string $environment = null): array
121
    {
122
        $requiredValues = $this->createUpperCaseResult(
123
            [$this->getEnvironment(), 'getRequiredVariables'],
124
            [$this->getEnvironment($environment), 'getRequiredVariables']
125
        );
126
127
        $result = [];
128
        foreach ($requiredValues as $name => $description) {
129
            $result[$name] = new RequiredValue($name, $description);
130
        }
131
132
        return $result;
133
    }
134
135
    /**
136
     * @return DotenvFile[]
137
     */
138
    public function getDotenvPaths(?string $environment = null): array
139
    {
140
        $paths = $this->createResult(
141
            [$this->getEnvironment(), 'getDotenvPaths'],
142
            [$this->getEnvironment($environment), 'getDotenvPaths']
143
        );
144
145
        return array_map(function (string $path): DotenvFile {
146
            return new DotenvFile($path);
147
        }, $paths);
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getHeader(): ?string
154
    {
155
        return $this->header;
156
    }
157
158
    /**
159
     * @return ConfigEnvironment[]
160
     */
161
    public function getEnvironments(): array
162
    {
163
        return $this->environments;
164
    }
165
166
    public function getDefaultEnvironment(): string
167
    {
168
        return $this->defaultEnvironment;
169
    }
170
171
172
    public function hasOption(string $name): bool
173
    {
174
        return in_array($name, $this->runtimeParameters->getAppParams(), true);
175
    }
176
177
    public function getImports(): array
178
    {
179
        return $this->getEnvironment()->getImports();
180
    }
181
182
    public function getScriptNames(): array
183
    {
184
        return $this->runtimeParameters->getCommands();
185
    }
186
187
    private function getParams(): array
188
    {
189
        return $this->runtimeParameters->getOverwrites();
190
    }
191
192
    private function createResult(callable ...$valueProviders): array
193
    {
194
        $mergedKeyValues = [];
195
196
        foreach ($valueProviders as $valueProvider) {
197
            foreach ($valueProvider() as $key => $value) {
198
                $mergedKeyValues[$key] = $value;
199
            }
200
        }
201
202
        return $mergedKeyValues;
203
    }
204
205
    private function createUpperCaseResult(callable ...$valueProviders): array
206
    {
207
        $mergedKeyValues = [];
208
209
        foreach ($valueProviders as $valueProvider) {
210
            foreach ($valueProvider() as $key => $value) {
211
                $mergedKeyValues[mb_strtoupper($key)] = $value;
212
            }
213
        }
214
215
        return $mergedKeyValues;
216
    }
217
218
    private function getEnvironment(?string $name = null): ConfigEnvironment
219
    {
220
        if ($name === null) {
221
            return $this->environments[$this->defaultEnvironment];
222
        }
223
224
        return $this->environments[$name];
225
    }
226
}
227