Completed
Pull Request — master (#84)
by Nick
01:45
created

Config::getDotenvPaths()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24

Duplication

Lines 9
Ratio 37.5 %

Importance

Changes 0
Metric Value
dl 9
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 8
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 ScriptPath[]
50
     */
51
    public function getAllScriptPaths(): array
52
    {
53
        $paths = [];
54
55 View Code Duplication
        foreach ($this->environments as $name => $environmentConfig) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            foreach ($environmentConfig->getAllScriptPaths() 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 ScriptPath($path, $name);
59
                } else {
60
                    $paths[] = new ScriptPath($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 $environmentString
107
     *
108
     * @return ScriptPath[]
109
     */
110
    public function getDotenvPaths(string $environmentString = null): array
111
    {
112
        $paths = [];
113
114
        $environments = [];
115
116
        $environments[$this->defaultEnvironment] = $this->getEnvironment($this->defaultEnvironment);
117
118
        if ($environmentString !== $this->defaultEnvironment) {
119
            $environments[$environmentString] = $this->getEnvironment($environmentString);
120
        }
121
122 View Code Duplication
        foreach ($environments as $name => $environmentConfig) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
            foreach ($environmentConfig->getDotenvPaths() as $path) {
124
                if ($name !== $this->defaultEnvironment) {
125
                    $paths[] = new ScriptPath($path, $name);
126
                } else {
127
                    $paths[] = new ScriptPath($path);
128
                }
129
            }
130
        }
131
132
        return $paths;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getHeader()
139
    {
140
        return $this->header;
141
    }
142
143
    /**
144
     * @return ConfigEnvironment[]
145
     */
146
    public function getEnvironments(): array
147
    {
148
        return $this->environments;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getDefaultEnvironment(): string
155
    {
156
        return $this->defaultEnvironment;
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    public function getParams() : array
163
    {
164
        return $this->params;
165
    }
166
167
    /**
168
     * @param callable[] ...$valueProviders
169
     * @return array
170
     */
171
    private function createResult(callable ...$valueProviders): array
172
    {
173
        $mergedKeyValues = [];
174
175
        foreach ($valueProviders as $valueProvider) {
176
            foreach ($valueProvider() as $key => $value) {
177
                $mergedKeyValues[$key] = $value;
178
            }
179
        }
180
181
        return $mergedKeyValues;
182
    }
183
184
    /**
185
     * @param string|null $name
186
     * @return ConfigEnvironment
187
     */
188
    private function getEnvironment(string $name = null): ConfigEnvironment
189
    {
190
        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...
191
            return $this->environments[$this->defaultEnvironment];
192
        }
193
194
        return $this->environments[$name];
195
    }
196
}
197