Passed
Pull Request — master (#6)
by Dmitriy
10:40
created

Params   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnvKey() 0 3 1
A calcValues() 0 3 1
B pushEnvVars() 0 22 7
A hasParams() 0 3 1
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($vars): array
16
    {
17
        $env = $this->builder->getConfig('dotenv')->getValues();
18
        if (!empty($vars)) {
19
            foreach ($vars as $key => &$value) {
20
                if (is_array($value)) {
21
                    foreach (array_keys($value) as $subkey) {
22
                        $envKey = $this->getEnvKey($key . '_' . $subkey);
23
                        if (isset($env[$envKey])) {
24
                            $value[$subkey] = $env[$envKey];
25
                        }
26
                    }
27
                } else {
28
                    $envKey = $this->getEnvKey($key);
29
                    if (isset($env[$envKey])) {
30
                        $vars[$key] = $env[$envKey];
31
                    }
32
                }
33
            }
34
        }
35
36
        return $vars;
37
    }
38
39
    private function getEnvKey(string $key): string
40
    {
41
        return strtoupper(strtr($key, '.-', '__'));
42
    }
43
44
    public function hasParams(): bool
45
    {
46
        return true;
47
    }
48
}
49