Completed
Push — master ( 8ed176...46d475 )
by Raffael
01:44
created

Environment   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 101
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 4 1
A map() 0 7 2
B variablesToTree() 0 38 6
1
<?php
2
declare(strict_types = 1);
3
4
/**
5
 * Micro
6
 *
7
 * @author    Fabian Jucker <[email protected]>
8
 * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com)
9
 * @license   MIT https://opensource.org/licenses/MIT
10
 */
11
12
namespace Micro\Config;
13
14
use \Micro\Config;
15
16
class Environment implements ConfigInterface
17
{
18
    /**
19
     * DELIMITER
20
     *
21
     * @var string
22
     */
23
    private const DELIMITER = '_';
24
25
    /**
26
     * Store
27
     *
28
     * @var Config
29
     */
30
    private $store;
31
32
    /**
33
     * Load config
34
     *
35
     * @param  string $config
36
     * @param  string $env
37
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
38
     */
39
    public function __construct(string $config = null, string $env = null)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $env is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        $this->store = $this->variablesToTree(array_merge($_ENV, $_SERVER));
42
    }
43
44
45
    /**
46
     * Get from config
47
     *
48
     * @param   string $name
49
     * @return  mixed
50
     */
51
    public function __get(string $name)
52
    {
53
        return $this->store->{$name};
54
    }
55
56
57
    /**
58
     * Get native config format as config instance
59
     *
60
     * @param   array $environmentVariables
61
     * @return  Config
62
     */
63
    public function map($environmentVariables = null): Config
64
    {
65
        if ($environmentVariables === null) {
66
            return $this->store;
67
        }
68
        return $this->variablesToTree($environmentVariables);
69
    }
70
71
72
    /**
73
     * Transform array of environment variables into a tree
74
     *
75
     * @param   array $environmentVars
76
     * @return  Config
77
     */
78
    protected function variablesToTree(array $environmentVars): Config
79
    {
80
        // transform keys to lowercase
81
        $environmentVars = array_change_key_case($environmentVars);
82
83
        // initialize root node
84
        $root = new Config();
85
        foreach ($environmentVars as $name => $value) {
86
            // split variable name by delimiter
87
            $name = explode(self::DELIMITER, $name);
88
89
            // go back to root
90
            $tree = $root;
91
92
            // iterate over key parts
93
            for ($i = 0; $i < count($name); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
94
                $key = $name[$i];
95
96
                // try to access subtree
97
                try {
98
                  // create new subtree if requested subtree already has a value
99
                  if (!is_a($tree->$key, '\Micro\Config')) {
100
                      $tree[$key] = new Config([$tree->$key]);
101
                  }
102
                  $tree = $tree->$key;
103
                } catch (Exception $e) {
104
                    // set value if last keypart or create subtree
105
                    if($i == (count($name) - 1)) {
106
                        $tree[$key] = $value;
107
                    } else {
108
                        $tree[$key] = new Config();
109
                        $tree = $tree->$key;
110
                    }
111
                }
112
            }
113
        }
114
        return $root;
115
    }
116
}
117