Completed
Push — master ( 1ece6c...656998 )
by Jan Philipp
13s queued 11s
created

Config::getDynamicVariables()   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
/**
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 ScriptsPath[]
50
     */
51
    public function getAllScriptsPaths(): array
52
    {
53
        $paths = [];
54
55
        foreach ($this->environments as $name => $environmentConfig) {
56
            foreach ($environmentConfig->getAllScriptsPaths() 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 ScriptsPath($path, $name);
59
                } else {
60
                    $paths[] = new ScriptsPath($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 $environment
107
     * @return DotenvFile[]
108
     */
109
    public function getDotenvPaths(string $environment = null): array
110
    {
111
        $paths = $this->createResult(
112
            [$this->getEnvironment(), 'getDotenvPaths'],
113
            [$this->getEnvironment($environment), 'getDotenvPaths']
114
        );
115
116
        return array_map(function (string $path): DotenvFile {
117
            return new DotenvFile($path);
118
        }, $paths);
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getHeader()
125
    {
126
        return $this->header;
127
    }
128
129
    /**
130
     * @return ConfigEnvironment[]
131
     */
132
    public function getEnvironments(): array
133
    {
134
        return $this->environments;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getDefaultEnvironment(): string
141
    {
142
        return $this->defaultEnvironment;
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function getParams() : array
149
    {
150
        return $this->params;
151
    }
152
153
    /**
154
     * @param callable[] ...$valueProviders
155
     * @return array
156
     */
157
    private function createResult(callable ...$valueProviders): array
158
    {
159
        $mergedKeyValues = [];
160
161
        foreach ($valueProviders as $valueProvider) {
162
            foreach ($valueProvider() as $key => $value) {
163
                $mergedKeyValues[$key] = $value;
164
            }
165
        }
166
167
        return $mergedKeyValues;
168
    }
169
170
    /**
171
     * @param string|null $name
172
     * @return ConfigEnvironment
173
     */
174
    private function getEnvironment(string $name = null): ConfigEnvironment
175
    {
176
        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...
177
            return $this->environments[$this->defaultEnvironment];
178
        }
179
180
        return $this->environments[$name];
181
    }
182
}
183