Completed
Push — master ( a19424...48cb06 )
by Auke
02:42 queued 13s
created

Configuration   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.15%

Importance

Changes 12
Bugs 2 Features 3
Metric Value
wmc 23
c 12
b 2
f 3
lcom 1
cbo 2
dl 0
loc 120
ccs 56
cts 65
cp 0.8615
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A cd() 0 4 1
A getVar() 0 4 1
A putVar() 0 4 1
A isHash() 0 4 2
A __construct() 0 5 1
A configure() 0 9 2
A ls() 0 6 1
A getValue() 0 13 3
A setValue() 0 17 4
A getValueIfRoot() 0 7 2
A mergeValue() 0 14 4
A acquire() 0 12 1
1
<?php
2
3
/*
4
 * This file is part of the Ariadne Component Library.
5
 *
6
 * (c) Muze <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace arc\config;
13
14
class Configuration implements ConfigurationInterface
15
{
16
    use \arc\traits\Proxy {
17
        \arc\traits\Proxy::__construct as private ProxyConstruct;
18
    }
19
20
    private $tree = null;
21
22 6
    public function __construct($tree)
23
    {
24 6
        $this->ProxyConstruct( $tree );
25 6
        $this->tree = $tree;
26 6
    }
27
28 6
    public function acquire($name)
29
    {
30 6
        return \arc\tree::dive(
31 6
            $this->tree,
32
            function ($node) use ($name) {
33 6
                return $this->getValueIfRoot( $name, $node->nodeValue );
34 6
            },
35
            function ($node, $result) use ($name) {
36 6
                return $this->mergeValue( $result, $this->getValue( $name, $node->nodeValue ) );
37 3
            }
38 3
        );
39
    }
40
41 6
    public function configure($name, $value)
42
    {
43 6
        if (!isset( $this->tree->nodeValue )) {
44 6
            $this->tree->nodeValue = array();
45 3
        }
46 6
        $this->setValue( $name, $value, $this->tree->nodeValue );
47
48 6
        return $this;
49
    }
50
51 4
    public function cd($path)
52
    {
53 4
        return new Configuration( $this->tree->cd( $path ) );
54
    }
55
56
    public function ls()
57
    {
58
        return $this->tree->ls( function ($node) {
59
            return new Configuration( $node );
60
        } );
61
    }
62
63
    // \arc\KeyValueStoreInterface
64
    public function getVar($name)
65
    {
66
        return $this->acquire( $name );
67
    }
68
69
    public function putVar($name, $value)
70
    {
71
        return $this->configure( $name, $value );
72
    }
73
74 6
    private function getValue($name, $config)
75
    {
76 6
        $vars = explode('.', $name);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
77 6
        $entry = $config;
78 6
        foreach ($vars as $var) {
79 6
            if (!isset( $entry[$var] )) {
80 2
                return null;
81
            }
82 6
            $entry = $entry[$var];
83 3
        }
84
85 6
        return $entry;
86
    }
87
88 6
    private function setValue($name, $value, &$config)
89
    {
90 6
        $vars = explode('.', $name);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
91 6
        $lastName = array_pop( $vars );
92 6
        $entry = &$config;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
93 6
        foreach ($vars as $var) {
94 6
            if (!isset( $entry[$var] )) {
95 6
                $entry[$var] = array();
96 3
            }
97 6
            $entry = &$entry[$var];
98 3
        }
99 6
        if ( !is_array( $entry ) ) {
100
            throw \arc\ConfigError( 'Unable to configure '.$name.', parent set to non-array value', \arc\exceptions::CONFIGURATION_ERROR );
101
        } else {
102 6
            $entry[ $lastName ] = $value;
103
        }
104 6
    }
105
106 6
    private function getValueIfRoot($name, $config)
107
    {
108 6
        $value = $this->getValue( $name, $config );
109 6
        if (!$this->isHash( $value )) {
110 4
            return $value;
111
        }
112 2
    }
113
114 6
    private function isHash($array)
115
    {
116 6
        return ( is_array( $array ) && !is_numeric( key( $array ) ) );
117
    }
118
119 6
    private function mergeValue($initial, $additional)
120
    {
121 6
        if (isset( $additional )) {
122 6
            if (!$this->isHash( $initial )) {
123 6
                return $additional;
124 2
            } elseif ( $this->isHash( $additional ) ) {
125 2
                return array_replace_recursive( $initial, $additional );
126
            } else {
127
                return $additional;
128
            }
129
        }
130
131 2
        return $initial;
132
    }
133
}
134