Completed
Pull Request — master (#109)
by Jan Philipp
01:37
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
     * @param ConfigEnvironment[] $environments
40
     */
41
    public function __construct(
42
        EnvironmentResolver $resolver,
43
        string $defaultEnvironment,
44
        array $environments,
45
        array $params,
46
        string $header = null
47
    ) {
48
        $this->resolver = $resolver;
49
        $this->defaultEnvironment = $defaultEnvironment;
50
        $this->environments = $environments;
51
        $this->params = $params;
52
        $this->header = $header;
53
    }
54
55
    /**
56
     * @return ScriptsPath[]
57
     */
58
    public function getAllScriptsPaths(): array
59
    {
60
        $paths = [];
61
62
        foreach ($this->environments as $name => $environmentConfig) {
63
            foreach ($environmentConfig->getAllScriptsPaths() as $path) {
64
                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...
65
                    $paths[] = new ScriptsPath($path, $environmentConfig->isHidden(), $name);
66
                } else {
67
                    $paths[] = new ScriptsPath($path, false);
68
                }
69
            }
70
        }
71
72
        return $paths;
73
    }
74
75
    public function getTemplates(string $environment = null): array
76
    {
77
        return $this->resolver->resolveTemplates($this->createResult(
78
            [$this->getEnvironment(), 'getTemplates'],
79
            [$this->getEnvironment($environment), 'getTemplates']
80
        ));
81
    }
82
83
    public function getDynamicVariables(string $environment = null): array
84
    {
85
        return $this->resolver->resolveVariables($this->createResult(
86
            [$this->getEnvironment(), 'getDynamicVariables'],
87
            [$this->getEnvironment($environment), 'getDynamicVariables']
88
        ));
89
    }
90
91
    public function getConstants(string $environment = null): array
92
    {
93
        return $this->resolver->resolveConstants($this->createResult(
94
            [$this->getEnvironment(), 'getConstants'],
95
            [$this->getEnvironment($environment), 'getConstants'],
96
            [$this, 'getParams']
97
        ));
98
    }
99
100
    public function getAllPlaceholders(string $environment = null): array
101
    {
102
        return array_merge(
103
            $this->getConstants($environment),
104
            $this->getDotenvVariables($environment),
105
            $this->getDynamicVariables($environment)
106
        );
107
    }
108
109
    /**
110
     * @return ValueProvider[]
111
     */
112
    public function getDotenvVariables(string $environment = null): array
113
    {
114
        $paths = $this->getDotenvPaths($environment);
115
116
        return $this->resolver->resolveDotenvVariables($paths);
117
    }
118
119
    /**
120
     * @return RequiredValue[]
121
     */
122
    public function getRequiredVariables(string $environment = null): array
123
    {
124
        $requiredValues = $this->createResult(
125
            [$this->getEnvironment(), 'getRequiredVariables'],
126
            [$this->getEnvironment($environment), 'getRequiredVariables']
127
        );
128
129
        $result = [];
130
        foreach ($requiredValues as $name => $description) {
131
            $result[$name] = new RequiredValue($name, $description);
132
        }
133
134
        return $result;
135
    }
136
137
    /**
138
     * @return DotenvFile[]
139
     */
140
    public function getDotenvPaths(string $environment = null): array
141
    {
142
        $paths = $this->createResult(
143
            [$this->getEnvironment(), 'getDotenvPaths'],
144
            [$this->getEnvironment($environment), 'getDotenvPaths']
145
        );
146
147
        return array_map(function (string $path): DotenvFile {
148
            return new DotenvFile($path);
149
        }, $paths);
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getHeader(): ?string
156
    {
157
        return $this->header;
158
    }
159
160
    /**
161
     * @return ConfigEnvironment[]
162
     */
163
    public function getEnvironments(): array
164
    {
165
        return $this->environments;
166
    }
167
168
    public function getDefaultEnvironment(): string
169
    {
170
        return $this->defaultEnvironment;
171
    }
172
173
    public function getParams(): array
174
    {
175
        return $this->params;
176
    }
177
178
    public function getImports()
179
    {
180
        return $this->getEnvironment()->getImports();
181
    }
182
183
    /**
184
     * @param callable[] ...$valueProviders
185
     */
186
    private function createResult(callable ...$valueProviders): array
187
    {
188
        $mergedKeyValues = [];
189
190
        foreach ($valueProviders as $valueProvider) {
191
            foreach ($valueProvider() as $key => $value) {
192
                $mergedKeyValues[$key] = $value;
193
            }
194
        }
195
196
        return $mergedKeyValues;
197
    }
198
199
    private function getEnvironment(string $name = null): ConfigEnvironment
200
    {
201
        if ($name === null) {
202
            return $this->environments[$this->defaultEnvironment];
203
        }
204
205
        return $this->environments[$name];
206
    }
207
}
208