Customizer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 50
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B customizeInternal() 0 15 8
A load() 0 3 1
A customize() 0 3 1
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