Params::pushEnvVars()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
dl 0
loc 22
ccs 0
cts 8
cp 0
rs 8.8333
cc 7
nc 2
nop 1
crap 56
1
<?php
2
/**
3
 * Composer plugin for config assembling
4
 *
5
 * @link      https://github.com/hiqdev/composer-config-plugin
6
 * @package   composer-config-plugin
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\composer\config\configs;
12
13
/**
14
 * Params class represents output configuration file with params definitions.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class Params extends Config
19
{
20
    protected function calcValues(array $sources): array
21
    {
22
        return $this->pushEnvVars(parent::calcValues($sources));
23
    }
24
25
    protected function pushEnvVars($vars): array
26
    {
27
        $env = $this->builder->getConfig('dotenv')->getValues();
28
        if (!empty($vars)) {
29
            foreach ($vars as $key => &$value) {
30
                if (is_array($value)) {
31
                    foreach (array_keys($value) as $subkey) {
32
                        $envKey = $this->getEnvKey($key . '_' . $subkey);
33
                        if (isset($env[$envKey])) {
34
                            $value[$subkey] = $env[$envKey];
35
                        }
36
                    }
37
                } else {
38
                    $envKey = $this->getEnvKey($key);
39
                    if (isset($env[$envKey])) {
40
                        $vars[$key] = $env[$envKey];
41
                    }
42
                }
43
            }
44
        }
45
46
        return $vars;
47
    }
48
49
    private function getEnvKey(string $key): string
50
    {
51
        return strtoupper(strtr($key, '.-', '__'));
52
    }
53
}
54