Customizer::load()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Wfs\CustomEnvironmentVariables;
4
5
use Noodlehaus\Config;
6
7
final class Customizer
8
{
9
    /**
10
     * @var Iterable
11
     */
12
    private $config;
13
14 1
    public static function load(string $filename): self
15
    {
16 1
        return new self(new Config($filename));
17
    }
18
19
    /**
20
     * Customizer constructor.
21
     * @param Iterable $config
22
     */
23 6
    public function __construct(Iterable $config)
24
    {
25 6
        $this->config = $config;
26 6
    }
27
28
    /**
29
     * @param Iterable $target
30
     * @param Iterable $config
31
     * @return Iterable
32
     */
33 6
    private function customizeInternal(Iterable $target, Iterable $config): Iterable
34
    {
35 6
        foreach ($config as $key => $value) {
36 6
            if (isset($target[$key])) {
37 6
                if (is_array($value) && is_array($target[$key])) {
38 6
                    $target[$key] = $this->customizeInternal($target[$key], $value);
39 5
                } elseif (is_string($target[$key]) && is_string($value)) {
40 5
                    $newValue = getenv($value);
41 5
                    if ($newValue) {
42 6
                        $target[$key] = $newValue;
43
                    }
44
                }
45
            }
46
        }
47 6
        return $target;
48
    }
49
50
    /**
51
     * @param Iterable $target
52
     * @return Iterable
53
     */
54 6
    public function customize(Iterable $target): Iterable
55
    {
56 6
        return $this->customizeInternal($target, $this->config);
57
    }
58
}
59