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

ProcessEnvironment   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 8
dl 0
loc 174
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A loadDotenvVariables() 0 7 1
A loadDotenvFile() 0 11 1
A diffDotenvVarsWithEnv() 0 12 2
A __construct() 0 7 1
A initializeConstants() 0 9 2
A initializeVariables() 0 10 2
A initializeDotenvVariables() 0 14 3
A initializeTemplates() 0 9 2
A getAllValues() 0 8 1
A getTemplates() 0 4 1
A createProcess() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\ScriptRuntime\Execution;
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\DotenvFile;
12
use Shopware\Psh\Config\ScriptsPath;
13
use Symfony\Component\Process\Process;
14
15
/**
16
 * Create representation of the current environment variables and constants
17
 */
18
class ProcessEnvironment
19
{
20
    /**
21
     * @var ValueProvider[]
22
     */
23
    private $constants;
24
25
    /**
26
     * @var ValueProvider[]
27
     */
28
    private $variables;
29
30
    /**
31
     * @var Template[]
32
     */
33
    private $templates;
34
35
    /**
36
     * @var array
37
     */
38
    private $dotenvVariables;
39
40
    /**
41
     * @param array $constants
42
     * @param array $variables
43
     * @param array $templates
44
     * @param ScriptsPath[] $dotenvPaths
45
     */
46
    public function __construct(array $constants, array $variables, array $templates, array $dotenvPaths)
47
    {
48
        $this->constants = $this->initializeConstants($constants);
49
        $this->variables = $this->initializeVariables($variables);
50
        $this->templates = $this->initializeTemplates($templates);
51
        $this->dotenvVariables = $this->initializeDotenvVariables($dotenvPaths);
0 ignored issues
show
Documentation introduced by
$dotenvPaths is of type array<integer,object<Sho...sh\Config\ScriptsPath>>, but the function expects a array<integer,object<Sho...Psh\Config\DotenvFile>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
    }
53
54
    /**
55
     * @param array $constants
56
     * @return ValueProvider[]
57
     */
58
    private function initializeConstants(array $constants): array
59
    {
60
        $resolvedValues = [];
61
        foreach ($constants as $name => $value) {
62
            $resolvedValues[$name] = new SimpleValueProvider((string) $value);
63
        }
64
65
        return $resolvedValues;
66
    }
67
68
    /**
69
     * @param array $variables
70
     * @return ValueProvider[]
71
     */
72
    private function initializeVariables(array $variables): array
73
    {
74
        $resolvedVariables = [];
75
        foreach ($variables as $name => $shellCommand) {
76
            $process = $this->createProcess($shellCommand);
77
            $resolvedVariables[$name] = new ProcessValueProvider($process);
78
        }
79
80
        return $resolvedVariables;
81
    }
82
83
    /**
84
     * @param DotenvFile[] $dotenvPaths
85
     * @return ValueProvider[]
86
     */
87
    private function initializeDotenvVariables(array $dotenvPaths): array
88
    {
89
        $variables = [];
90
91
        foreach ($dotenvPaths as $dotenvPath) {
92
            $dotenvVariables = $this->loadDotenvVariables($dotenvPath);
93
94
            foreach ($dotenvVariables as $variableKey => $variableValue) {
95
                $variables[$variableKey] = new SimpleValueProvider($variableValue);
96
            }
97
        }
98
99
        return $variables;
100
    }
101
102
    /**
103
     * @param array $templates
104
     * @return Template[]
105
     */
106
    private function initializeTemplates(array $templates): array
107
    {
108
        $resolvedVariables = [];
109
        foreach ($templates as $template) {
110
            $resolvedVariables[] = new Template($template['source'], $template['destination']);
111
        }
112
113
        return $resolvedVariables;
114
    }
115
116
    /**
117
     * @return ValueProvider[]
118
     */
119
    public function getAllValues(): array
120
    {
121
        return array_merge(
122
            $this->constants,
123
            $this->dotenvVariables,
124
            $this->variables
125
        );
126
    }
127
128
    /**
129
     * @return Template[]
130
     */
131
    public function getTemplates(): array
132
    {
133
        return $this->templates;
134
    }
135
136
    /**
137
     * @param string $shellCommand
138
     * @return Process
139
     */
140
    public function createProcess(string $shellCommand): Process
141
    {
142
        return new Process($shellCommand);
143
    }
144
145
    /**
146
     * @param DotenvFile $dotenvPath
147
     * @return array
148
     */
149
    private function loadDotenvVariables(DotenvFile $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 DotenvFile $dotenvPath
159
     * @param DotenvFactory $dotenvFactory
160
     * @return array
161
     */
162
    private function loadDotenvFile(DotenvFile $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