Completed
Pull Request — master (#86)
by Jan Philipp
01:42
created

ProcessEnvironment::__construct()   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 4
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\ScriptRuntime;
4
5
use function array_merge;
6
use Dotenv\Dotenv;
7
use Dotenv\Environment\DotenvFactory;
8
use function pathinfo;
9
use const PATHINFO_BASENAME;
10
use const PATHINFO_DIRNAME;
11
use Shopware\Psh\Config\ScriptPath;
12
use Symfony\Component\Process\Process;
13
14
/**
15
 * Create representation of the current environment variables and constants
16
 */
17
class ProcessEnvironment
18
{
19
    /**
20
     * @var ValueProvider[]
21
     */
22
    private $constants;
23
24
    /**
25
     * @var ValueProvider[]
26
     */
27
    private $variables;
28
29
    /**
30
     * @var Template[]
31
     */
32
    private $templates;
33
34
    /**
35
     * @var array
36
     */
37
    private $dotenvVariables;
38
39
    /**
40
     * @param array $constants
41
     * @param array $variables
42
     * @param array $templates
43
     * @param array $dotenvPaths
44
     */
45
    public function __construct(array $constants, array $variables, array $templates, array $dotenvPaths)
46
    {
47
        $this->constants = $this->initializeConstants($constants);
48
        $this->variables = $this->initializeVariables($variables);
49
        $this->templates = $this->initializeTemplates($templates);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->initializeTemplates($templates) of type array<integer,object<Sho...Runtime\ValueProvider>> is incompatible with the declared type array<integer,object<Sho...criptRuntime\Template>> of property $templates.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
        $this->dotenvVariables = $this->initializeDotenvVariables($dotenvPaths);
51
    }
52
53
    /**
54
     * @param array $constants
55
     * @return ValueProvider[]
56
     */
57
    private function initializeConstants(array $constants): array
58
    {
59
        $resolvedValues = [];
60
        foreach ($constants as $name => $value) {
61
            $resolvedValues[$name] = new SimpleValueProvider((string) $value);
62
        }
63
64
        return $resolvedValues;
65
    }
66
67
    /**
68
     * @param array $variables
69
     * @return ValueProvider[]
70
     */
71
    private function initializeVariables(array $variables): array
72
    {
73
        $resolvedVariables = [];
74
        foreach ($variables as $name => $shellCommand) {
75
            $process = $this->createProcess($shellCommand);
76
            $resolvedVariables[$name] = new ProcessValueProvider($process);
77
        }
78
79
        return $resolvedVariables;
80
    }
81
82
    /**
83
     * @param ScriptPath[] $dotenvPaths
84
     * @return ValueProvider[]
85
     */
86
    private function initializeDotenvVariables(array $dotenvPaths): array
87
    {
88
        $variables = [];
89
90
        foreach ($dotenvPaths as $dotenvPath) {
91
            $dotenvVariables = $this->loadDotenvVariables($dotenvPath);
92
93
            foreach ($dotenvVariables as $variableKey => $variableValue) {
94
                $variables[$variableKey] = new SimpleValueProvider($variableValue);
95
            }
96
        }
97
98
        return $variables;
99
    }
100
101
    /**
102
     * @param array $templates
103
     * @return ValueProvider[]
104
     */
105
    private function initializeTemplates(array $templates): array
106
    {
107
        $resolvedVariables = [];
108
        foreach ($templates as $template) {
109
            $resolvedVariables[] = new Template($template['source'], $template['destination']);
110
        }
111
112
        return $resolvedVariables;
113
    }
114
115
    /**
116
     * @return ValueProvider[]
117
     */
118
    public function getAllValues(): array
119
    {
120
        return array_merge(
121
            $this->constants,
122
            $this->dotenvVariables,
123
            $this->variables
124
        );
125
    }
126
127
    /**
128
     * @return Template[]
129
     */
130
    public function getTemplates(): array
131
    {
132
        return $this->templates;
133
    }
134
135
    /**
136
     * @param string $shellCommand
137
     * @return Process
138
     */
139
    public function createProcess(string $shellCommand): Process
140
    {
141
        return new Process($shellCommand);
142
    }
143
144
    /**
145
     * @param ScriptPath $dotenvPath
146
     *
147
     * @return array
148
     */
149
    private function loadDotenvVariables(ScriptPath $dotenvPath): array
150
    {
151
        $dotenvFactory = new DotenvFactory();
152
153
        $fileData = $this->loadDotenvFile($dotenvPath, $dotenvFactory);
154
        return $this->diffDotenvVarsWithEnv($fileData, $dotenvFactory);
155
    }
156
157
    /**
158
     * @param ScriptPath $dotenvPath
159
     * @param DotenvFactory $dotenvFactory
160
     * @return array
161
     */
162
    private function loadDotenvFile(ScriptPath $dotenvPath, DotenvFactory $dotenvFactory): array
163
    {
164
        $filePath = $dotenvPath->getPath();
165
        $dotenv = Dotenv::create(
166
            pathinfo($filePath, PATHINFO_DIRNAME),
167
            pathinfo($filePath, PATHINFO_BASENAME),
168
            $dotenvFactory
169
        );
170
171
        return $dotenv->load();
172
    }
173
174
    /**
175
     * @param array $fileData
176
     * @param DotenvFactory $dotenvFactory
177
     * @return array
178
     */
179
    private function diffDotenvVarsWithEnv(array $fileData, DotenvFactory $dotenvFactory): array
180
    {
181
        $fileKeys = array_keys($fileData);
182
        $result = [];
183
184
        $existingVariables = $dotenvFactory->createImmutable();
185
        foreach ($fileKeys as $key) {
186
            $result[$key] = $existingVariables->get($key);
187
        }
188
189
        return $result;
190
    }
191
}
192