Passed
Pull Request — master (#8)
by Dmitriy
28:38 queued 13:30
created

Params::pushEnvVars()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 5
nop 1
dl 0
loc 28
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Composer\Config\Configs;
4
5
/**
6
 * Params class represents output configuration file with params definitions.
7
 */
8
class Params extends Config
9
{
10
    protected function calcValues(array $sources): array
11
    {
12
        return $this->pushEnvVars(parent::calcValues($sources));
13
    }
14
15
    protected function pushEnvVars(array $vars): array
16
    {
17
        if ($vars === []) {
18
            return [];
19
        }
20
21
        $env = array_merge(
22
            $this->builder->getConfig('envs')->getValues(),
23
            $this->builder->getConfig('dotenv')->getValues()
24
        );
25
26
        foreach ($vars as $key => &$value) {
27
            if (is_array($value)) {
28
                foreach (array_keys($value) as $subkey) {
29
                    $envKey = $this->getEnvKey($key . '_' . $subkey);
30
                    if (isset($env[$envKey])) {
31
                        $value[$subkey] = $env[$envKey];
32
                    }
33
                }
34
            } else {
35
                $envKey = $this->getEnvKey($key);
36
                if (isset($env[$envKey])) {
37
                    $vars[$key] = $env[$envKey];
38
                }
39
            }
40
        }
41
42
        return $vars;
43
    }
44
45
    private function getEnvKey(string $key): string
46
    {
47
        return strtoupper(strtr($key, '.-', '__'));
48
    }
49
50
    public function paramsRequires(): bool
51
    {
52
        return false;
53
    }
54
}
55