Completed
Pull Request — master (#42)
by Simon
01:51
created

Config::getDefaultEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * @param string|null $header
27
     * @param string $defaultEnvironment
28
     * @param ConfigEnvironment[] $environments
29
     */
30
    public function __construct(
31
        string $header = null,
32
        string $defaultEnvironment,
33
        array $environments
34
    ) {
35
        $this->header = $header;
36
        $this->defaultEnvironment = $defaultEnvironment;
37
        $this->environments = $environments;
38
    }
39
40
    /**
41
     * @return ScriptPath[]
42
     */
43
    public function getAllScriptPaths(): array
44
    {
45
        $paths = [];
46
47
        foreach ($this->environments as $name => $environmentConfig) {
48
            foreach ($environmentConfig->getAllScriptPaths() as $path) {
49
                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...
50
                    $paths[] = new ScriptPath($path, $name);
51
                } else {
52
                    $paths[] = new ScriptPath($path);
53
                }
54
            }
55
        }
56
57
        return $paths;
58
    }
59
60
    /**
61
     * @param string|null $environment
62
     * @return array
63
     */
64
    public function getTemplates(string $environment = null): array
65
    {
66
        return $this->createResult(
67
            [$this->getEnvironment(), 'getTemplates'],
68
            [$this->getEnvironment($environment), 'getTemplates']
69
        );
70
    }
71
72
    /**
73
     * @param string|null $environment
74
     * @return array
75
     */
76
    public function getDynamicVariables(string $environment = null): array
77
    {
78
        return $this->createResult(
79
            [$this->getEnvironment(), 'getDynamicVariables'],
80
            [$this->getEnvironment($environment), 'getDynamicVariables']
81
        );
82
    }
83
84
85
    /**
86
     * @param string|null $environment
87
     * @return array
88
     */
89
    public function getConstants(string $environment = null): array
90
    {
91
        return $this->createResult(
92
            [$this->getEnvironment(), 'getConstants'],
93
            [$this->getEnvironment($environment), 'getConstants']
94
        );
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getHeader()
101
    {
102
        return $this->header;
103
    }
104
105
    /**
106
     * @return ConfigEnvironment[]
107
     */
108
    public function getEnvironments(): array
109
    {
110
        return $this->environments;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getDefaultEnvironment(): string
117
    {
118
        return $this->defaultEnvironment;
119
    }
120
121
    /**
122
     * @param callable $defaultValues
123
     * @param callable $specificValues
124
     * @return array
125
     */
126
    private function createResult(callable $defaultValues, callable $specificValues): array
127
    {
128
        $mergedKeyValues = [];
129
130
        foreach ($defaultValues() as $key => $value) {
131
            $mergedKeyValues[$key] = $value;
132
        }
133
134
        foreach ($specificValues() as $key => $value) {
135
            $mergedKeyValues[$key] = $value;
136
        }
137
138
        return $mergedKeyValues;
139
    }
140
141
    /**
142
     * @param string|null $name
143
     * @return ConfigEnvironment
144
     */
145
    private function getEnvironment(string $name = null): ConfigEnvironment
146
    {
147
        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...
148
            return $this->environments[$this->defaultEnvironment];
149
        }
150
151
        return $this->environments[$name];
152
    }
153
}
154