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

Params   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A calcValues() 0 3 1
A paramsRequires() 0 3 1
A getEnvKey() 0 3 1
B pushEnvVars() 0 28 7
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