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

ConfigMerger::mergeImport()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
use function array_keys;
6
use function array_merge;
7
8
class ConfigMerger
9
{
10
    public function mergeOverride(Config $config, Config $override = null): Config
11
    {
12
        if ($override === null) {
13
            return $config;
14
        }
15
16
        $header = $config->getHeader();
17
        $defaultEnvironment = $config->getDefaultEnvironment();
18
        $environments = $config->getEnvironments();
19
20
        if ($override->getHeader()) {
21
            $header = $override->getHeader();
22
        }
23
24
        if ($override->getEnvironments()) {
25
            $environments = $this->mergeConfigEnvironments($config, $override);
26
        }
27
28
        if ($override->getDefaultEnvironment()) {
29
            $defaultEnvironment = $override->getDefaultEnvironment();
30
        }
31
32
        return new Config(new EnvironmentResolver(), $defaultEnvironment, $environments, $config->getParams(), $header);
33
    }
34
35
    public function mergeImport(Config $config, Config $import = null): Config
36
    {
37
        if ($import === null) {
38
            return $config;
39
        }
40
41
        $header = $config->getHeader();
42
        $defaultEnvironment = $config->getDefaultEnvironment();
43
        $environments = $config->getEnvironments();
44
45
        if ($import->getEnvironments()) {
46
            $environments = $this->mergeConfigEnvironments($config, $import, true);
47
        }
48
49
        return new Config(new EnvironmentResolver(), $defaultEnvironment, $environments, $config->getParams(), $header);
50
    }
51
52
    private function mergeConfigEnvironments(Config $config, Config $override, bool $asImport = false): array
53
    {
54
        $environments = [];
55
56
        $foundEnvironments = array_keys(array_merge($config->getEnvironments(), $override->getEnvironments()));
57
58
        foreach ($foundEnvironments as $name) {
59 View Code Duplication
            if (!isset($override->getEnvironments()[$name])) {
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...
60
                $environments[$name] = $config->getEnvironments()[$name];
61
62
                continue;
63
            }
64
65 View Code Duplication
            if (!isset($config->getEnvironments()[$name])) {
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...
66
                $environments[$name] = $override->getEnvironments()[$name];
67
68
                continue;
69
            }
70
71
            if ($asImport) {
72
                $environments[$name] = $this
73
                    ->mergeEnvironmentsAsImport($config->getEnvironments()[$name], $override->getEnvironments()[$name]);
74
            } else {
75
                $environments[$name] = $this
76
                    ->mergeEnvironmentsAsOverride($config->getEnvironments()[$name], $override->getEnvironments()[$name]);
77
            }
78
        }
79
80
        return $environments;
81
    }
82
83 View Code Duplication
    private function mergeEnvironmentsAsOverride(ConfigEnvironment $original, ConfigEnvironment $override): ConfigEnvironment
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
84
    {
85
        return new ConfigEnvironment(
86
            $this->overrideHidden($original, $override),
87
            $this->overrideScriptsPaths($original, $override),
88
            $this->mergeDynamicVariables($original, $override),
89
            $this->mergeConstants($original, $override),
90
            $this->overrideTemplates($original, $override),
91
            $this->mergeDotenvPaths($original, $override)
92
        );
93
    }
94
95 View Code Duplication
    private function mergeEnvironmentsAsImport(ConfigEnvironment $original, ConfigEnvironment $override): ConfigEnvironment
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
96
    {
97
        return new ConfigEnvironment(
98
            $this->overrideHidden($original, $override),
99
            $this->mergeScriptsPaths($original, $override),
100
            $this->mergeDynamicVariables($original, $override),
101
            $this->mergeConstants($original, $override),
102
            $this->mergeTemplates($original, $override),
103
            $this->mergeDotenvPaths($original, $override)
104
        );
105
    }
106
107
    private function mergeDynamicVariables(ConfigEnvironment $configEnvironment, ConfigEnvironment $overrideEnv): array
108
    {
109
        return array_merge($configEnvironment->getDynamicVariables(), $overrideEnv->getDynamicVariables());
110
    }
111
112
    /**
113
     * @return ScriptsPath[]
114
     */
115
    private function mergeDotenvPaths(ConfigEnvironment $configEnvironment, ConfigEnvironment $overrideConfigEnv): array
116
    {
117
        return array_merge($configEnvironment->getDotenvPaths(), $overrideConfigEnv->getDotenvPaths());
118
    }
119
120
    /**
121
     * @return ScriptsPath[]
122
     */
123
    private function mergeScriptsPaths(ConfigEnvironment $configEnvironment, ConfigEnvironment $overrideConfigEnv): array
124
    {
125
        return array_merge($configEnvironment->getAllScriptsPaths(), $overrideConfigEnv->getAllScriptsPaths());
126
    }
127
128
    /**
129
     * @return ScriptsPath[]
130
     */
131
    private function overrideScriptsPaths(ConfigEnvironment $configEnvironment, ConfigEnvironment $overrideConfigEnv): array
132
    {
133
        if ($overrideConfigEnv->getAllScriptsPaths()) {
134
            return $overrideConfigEnv->getAllScriptsPaths();
135
        }
136
137
        return $configEnvironment->getAllScriptsPaths();
138
    }
139
140
    private function mergeConstants(ConfigEnvironment $configEnvironment, ConfigEnvironment $overrideConfigEnv): array
141
    {
142
        return array_merge($configEnvironment->getConstants(), $overrideConfigEnv->getConstants());
143
    }
144
145
    /**
146
     * @param $overrideConfigEnv
147
     */
148
    private function overrideTemplates(ConfigEnvironment $configEnvironment, ConfigEnvironment $overrideConfigEnv): array
149
    {
150
        if ($overrideConfigEnv->getTemplates()) {
151
            return $overrideConfigEnv->getTemplates();
152
        }
153
154
        return $configEnvironment->getTemplates();
155
    }
156
157
    private function mergeTemplates(ConfigEnvironment $configEnvironment, ConfigEnvironment $overrideConfigEnv): array
158
    {
159
        return array_merge($configEnvironment->getTemplates(), $overrideConfigEnv->getTemplates());
160
    }
161
162
    private function overrideHidden(ConfigEnvironment $originalConfigEnv, ConfigEnvironment $overrideEnv): bool
163
    {
164
        if ($overrideEnv->isHidden()) {
165
            return true;
166
        }
167
168
        return $originalConfigEnv->isHidden();
169
    }
170
}
171