Completed
Push — master ( d234f3...0a4926 )
by Jan Philipp
47s queued 12s
created

EnvironmentResolver::resolveTemplates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
use Dotenv\Dotenv;
6
use Dotenv\Repository\Adapter\MultiReader;
7
use Dotenv\Repository\Adapter\PutenvAdapter;
8
use Dotenv\Repository\Adapter\ServerConstAdapter;
9
use Symfony\Component\Process\Process;
10
use function array_keys;
11
use function pathinfo;
12
13
class EnvironmentResolver
14
{
15
    /**
16
     * @param DotenvFile[] $dotenvPaths
17
     * @return ValueProvider[]
18
     */
19
    public function resolveDotenvVariables(array $dotenvPaths): array
20
    {
21
        $variables = [];
22
23
        foreach ($dotenvPaths as $dotenvPath) {
24
            $dotenvVariables = $this->loadDotenvVariables($dotenvPath);
25
26
            foreach ($dotenvVariables as $variableKey => $variableValue) {
27
                $variables[$variableKey] = new SimpleValueProvider($variableValue);
28
            }
29
        }
30
31
        return $variables;
32
    }
33
34
    private function loadDotenvVariables(DotenvFile $dotenvPath): array
35
    {
36
        $fileData = $this->loadEnvVarsFromDotenvFile($dotenvPath);
37
38
        return $this->diffDotenvVarsWithCurrentApplicationEnv($fileData);
39
    }
40
41
    private function loadEnvVarsFromDotenvFile(DotenvFile $dotenvPath): array
42
    {
43
        $filePath = $dotenvPath->getPath();
44
45
        $dotenv = Dotenv::createArrayBacked(
46
            pathinfo($filePath, \PATHINFO_DIRNAME),
47
            pathinfo($filePath, \PATHINFO_BASENAME)
48
        );
49
50
        return $dotenv->load();
51
    }
52
53
    private function diffDotenvVarsWithCurrentApplicationEnv(array $fileData): array
54
    {
55
        $fileKeys = array_keys($fileData);
56
        $result = [];
57
58
        $reader = new MultiReader([
59
            PutenvAdapter::create()->get(),
60
            ServerConstAdapter::create()->get(),
61
        ]);
62
63
        foreach ($fileKeys as $key) {
64
            $option = $reader->read($key);
65
66
            if ($option->isEmpty()) {
67
                $result[$key] = $fileData[$key];
68
                continue;
69
            }
70
71
            $result[$key] = $option->get();
72
        }
73
74
        return $result;
75
    }
76
77
    /**
78
     * @return ValueProvider[]
79
     */
80
    public function resolveConstants(array $constants): array
81
    {
82
        $resolvedValues = [];
83
        foreach ($constants as $name => $value) {
84
            $resolvedValues[$name] = new SimpleValueProvider((string) $value);
85
        }
86
87
        return $resolvedValues;
88
    }
89
90
    /**
91
     * @return ValueProvider[]
92
     */
93
    public function resolveVariables(array $variables): array
94
    {
95
        $resolvedVariables = [];
96
        foreach ($variables as $name => $shellCommand) {
97
            $process = $this->createProcess($shellCommand);
98
            $resolvedVariables[$name] = new ProcessValueProvider($process);
99
        }
100
101
        return $resolvedVariables;
102
    }
103
104
    /**
105
     * @return Template[]
106
     */
107
    public function resolveTemplates(array $templates): array
108
    {
109
        $resolvedVariables = [];
110
        foreach ($templates as $template) {
111
            $resolvedVariables[] = new Template($template['source'], $template['destination']);
112
        }
113
114
        return $resolvedVariables;
115
    }
116
117
    private function createProcess(string $shellCommand): Process
118
    {
119
        return new Process($shellCommand);
0 ignored issues
show
Documentation introduced by
$shellCommand is of type string, but the function expects a array.

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...
120
    }
121
}
122