Completed
Pull Request — master (#111)
by Jan Philipp
01:45
created

Config::getTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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