Completed
Pull Request — master (#109)
by Jan Philipp
01:40
created

Config::getRequiredVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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