Completed
Pull Request — master (#84)
by Nick
02:29
created

ProcessEnvironment::getDotenvVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\ScriptRuntime;
4
5
use function array_merge;
6
use function dirname;
7
use Dotenv\Dotenv;
8
use Dotenv\Environment\DotenvFactory;
9
use function pathinfo;
10
use const PATHINFO_DIRNAME;
11
use const PATHINFO_EXTENSION;
12
use const PATHINFO_FILENAME;
13
use Shopware\Psh\Config\ScriptPath;
14
use Symfony\Component\Process\Process;
15
16
/**
17
 * Create representation of the current environment variables and constants
18
 */
19
class ProcessEnvironment
20
{
21
    /**
22
     * @var ValueProvider[]
23
     */
24
    private $constants;
25
26
    /**
27
     * @var ValueProvider[]
28
     */
29
    private $variables;
30
31
    /**
32
     * @var Template[]
33
     */
34
    private $templates;
35
36
    /**
37
     * @var array
38
     */
39
    private $dotenvVariables;
40
41
    /**
42
     * @param array $constants
43
     * @param array $variables
44
     * @param array $templates
45
     * @param array $dotenvPaths
46
     */
47
    public function __construct(array $constants, array $variables, array $templates, array $dotenvPaths)
48
    {
49
        $this->constants = $this->initializeConstants($constants);
50
        $this->variables = $this->initializeVariables($variables);
51
        $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...
52
        $this->dotenvVariables = $this->initializeDotenvVariables($dotenvPaths);
53
    }
54
55
    /**
56
     * @param array $constants
57
     * @return ValueProvider[]
58
     */
59
    private function initializeConstants(array $constants): array
60
    {
61
        $resolvedValues = [];
62
        foreach ($constants as $name => $value) {
63
            $resolvedValues[$name] = new SimpleValueProvider((string) $value);
64
        }
65
66
        return $resolvedValues;
67
    }
68
69
    /**
70
     * @param array $variables
71
     * @return ValueProvider[]
72
     */
73
    private function initializeVariables(array $variables): array
74
    {
75
        $resolvedVariables = [];
76
        foreach ($variables as $name => $shellCommand) {
77
            $process = $this->createProcess($shellCommand);
78
            $resolvedVariables[$name] = new ProcessValueProvider($process);
79
        }
80
81
        return $resolvedVariables;
82
    }
83
84
    /**
85
     * @param ScriptPath[] $dotenvPaths
86
     *
87
     * @return array
88
     */
89
    private function initializeDotenvVariables(array $dotenvPaths): array
90
    {
91
        $variables = [];
92
93
        foreach ($dotenvPaths as $dotenvPath) {
94
            $dotenvVariables = $this->getDotenvVariables($dotenvPath);
95
96
            foreach ($dotenvVariables as $variableKey => $variableValue) {
97
                $variables[$variableKey] = new SimpleValueProvider($variableValue);
98
            }
99
        }
100
101
        return $variables;
102
    }
103
104
    /**
105
     * @param array $templates
106
     * @return ValueProvider[]
107
     */
108
    private function initializeTemplates(array $templates): array
109
    {
110
        $resolvedVariables = [];
111
        foreach ($templates as $template) {
112
            $resolvedVariables[] = new Template($template['source'], $template['destination']);
113
        }
114
115
        return $resolvedVariables;
116
    }
117
118
    /**
119
     * @return ValueProvider[]
120
     */
121
    public function getAllValues(): array
122
    {
123
        return array_merge(
124
            $this->constants,
125
            $this->variables,
126
            $this->dotenvVariables
127
        );
128
    }
129
130
    /**
131
     * @return Template[]
132
     */
133
    public function getTemplates(): array
134
    {
135
        return $this->templates;
136
    }
137
138
    /**
139
     * @param string $shellCommand
140
     * @return Process
141
     */
142
    public function createProcess(string $shellCommand): Process
143
    {
144
        return new Process($shellCommand);
145
    }
146
147
    /**
148
     * @param ScriptPath $dotenvPath
149
     *
150
     * @return array
151
     */
152
    private function getDotenvVariables(ScriptPath $dotenvPath): array
153
    {
154
        $filePath = $dotenvPath->getPath();
155
156
        $fileName = pathinfo($filePath, PATHINFO_FILENAME);
157
        $extension = pathinfo($filePath, PATHINFO_EXTENSION);
158
159
        if ($extension) {
160
            $fileName .= '.' . $extension;
161
        }
162
163
        $dotenv = Dotenv::create(
164
            pathinfo($filePath, PATHINFO_DIRNAME),
165
            $fileName,
166
            new DotenvFactory()
167
        );
168
169
        return $dotenv->load();
170
    }
171
}
172