Completed
Push — master ( 0a4926...d192d3 )
by Jan Philipp
24s queued 11s
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 mb_strtoupper;
12
use function pathinfo;
13
14
class EnvironmentResolver
15
{
16
    /**
17
     * @param DotenvFile[] $dotenvPaths
18
     * @return ValueProvider[]
19
     */
20
    public function resolveDotenvVariables(array $dotenvPaths): array
21
    {
22
        $variables = [];
23
24
        foreach ($dotenvPaths as $dotenvPath) {
25
            $dotenvVariables = $this->loadDotenvVariables($dotenvPath);
26
27
            foreach ($dotenvVariables as $variableKey => $variableValue) {
28
                $variables[mb_strtoupper($variableKey)] = new SimpleValueProvider($variableValue);
29
            }
30
        }
31
32
        return $variables;
33
    }
34
35
    private function loadDotenvVariables(DotenvFile $dotenvPath): array
36
    {
37
        $fileData = $this->loadEnvVarsFromDotenvFile($dotenvPath);
38
39
        return $this->diffDotenvVarsWithCurrentApplicationEnv($fileData);
40
    }
41
42
    private function loadEnvVarsFromDotenvFile(DotenvFile $dotenvPath): array
43
    {
44
        $filePath = $dotenvPath->getPath();
45
46
        $dotenv = Dotenv::createArrayBacked(
47
            pathinfo($filePath, \PATHINFO_DIRNAME),
48
            pathinfo($filePath, \PATHINFO_BASENAME)
49
        );
50
51
        return $dotenv->load();
52
    }
53
54
    private function diffDotenvVarsWithCurrentApplicationEnv(array $fileData): array
55
    {
56
        $fileKeys = array_keys($fileData);
57
        $result = [];
58
59
        $reader = new MultiReader([
60
            PutenvAdapter::create()->get(),
61
            ServerConstAdapter::create()->get(),
62
        ]);
63
64
        foreach ($fileKeys as $key) {
65
            $option = $reader->read($key);
66
67
            if ($option->isEmpty()) {
68
                $result[$key] = $fileData[$key];
69
                continue;
70
            }
71
72
            $result[$key] = $option->get();
73
        }
74
75
        return $result;
76
    }
77
78
    /**
79
     * @return ValueProvider[]
80
     */
81
    public function resolveConstants(array $constants): array
82
    {
83
        $resolvedValues = [];
84
        foreach ($constants as $name => $value) {
85
            $resolvedValues[$name] = new SimpleValueProvider((string) $value);
86
        }
87
88
        return $resolvedValues;
89
    }
90
91
    /**
92
     * @return ValueProvider[]
93
     */
94
    public function resolveVariables(array $variables): array
95
    {
96
        $resolvedVariables = [];
97
        foreach ($variables as $name => $shellCommand) {
98
            $process = $this->createProcess($shellCommand);
99
            $resolvedVariables[$name] = new ProcessValueProvider($process);
100
        }
101
102
        return $resolvedVariables;
103
    }
104
105
    private function createProcess(string $shellCommand): Process
106
    {
107
        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...
108
    }
109
}
110