Completed
Push — master ( f6802b...5d7281 )
by Marco
11:02
created

ConfigurationParametersTrait::getFromParts()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 35
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
ccs 9
cts 9
cp 1
rs 8.5806
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php namespace Comodojo\Foundation\Base;
2
3
use \InvalidArgumentException;
4
5
/**
6
 * @package     Comodojo Foundation
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
trait ConfigurationParametersTrait {
22
23
    protected $parameters = [];
24
25 5
    public function get($parameter=null) {
26
27 5
        if ( is_null($parameter) ) return $this->parameters;
28
29 4
        return $this->getFromParts(self::splitParts($parameter));
30
31
    }
32
33 2
    public function set($parameter, $value) {
34
35 2
        $parts = self::splitParts($parameter);
36
37 2
        if ( empty($parts) ) throw new InvalidArgumentException("Invalid parameter $parameter");
38
39 2
        $this->setFromParts($parts, $value);
40
41 2
        return $this;
42
43
    }
44
45 3
    public function has($parameter) {
46
47 3
        return is_null($this->getFromParts(self::splitParts($parameter))) ? false : true;
48
49
    }
50
51 3
    public function delete($parameter = null) {
52
53 3
        if ( is_null($parameter) ) {
54
55 1
            $this->parameters = [];
56 1
            return true;
57
58
        }
59
60 2
        $parts = self::splitParts($parameter);
61
62 2
        if ( empty($parts) ) throw new InvalidArgumentException("Invalid parameter $parameter");
63
64 2
        return $this->deleteFromParts($parts);
65
66
    }
67
68 6
    protected function getFromParts(array $parts) {
69
70 6
        if ( empty($parts) ) return null;
71
72 6
        $reference = &$this->parameters;
73
74 6
        foreach ($parts as $part) {
75
76 6
            if ( !isset($reference[$part]) ) {
77 4
                return null;
78
            }
79
80 5
            $reference = &$reference[$part];
81
82
        }
83
84 5
        $data = $reference;
85
86 5
        return $data;
87
        //
88
        // $data = $this->parameters;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
        //
90
        // foreach ($parts as $part) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
        //
92
        //     if ( isset($data[$part]) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
        //         $data = $data[$part];
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
        //     } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
        //         return null;
96
        //     }
97
        //
98
        // }
99
        //
100
        // return $data;
101
102
    }
103
104 2
    protected function setFromParts(array $parts, $value) {
105
106 2
        $reference = &$this->parameters;
107
108 2
        foreach ($parts as $part) {
109
110 2
            if ( !isset($reference[$part]) ) {
111 2
                $reference[$part] = [];
112
            }
113
114 2
            $reference = &$reference[$part];
115
116
        }
117
118 2
        $reference = $value;
119
120 2
        return true;
121
122
    }
123
124 2
    protected function deleteFromParts(array $parts) {
125
126 2
        $reference = &$this->parameters;
127 2
        $l = count($parts);
128
129 2
        for ($i=0; $i < $l; $i++) {
130 2
            if ( !isset($reference[$parts[$i]]) ) {
131
                return false;
132
            }
133 2
            if ($i == $l-1) {
134 2
                unset($reference[$parts[$i]]);
135
            } else {
136 1
                $reference = &$reference[$parts[$i]];
137
            }
138
        }
139
140 2
        return true;
141
142
    }
143
144 6
    protected static function splitParts($parameter) {
145
146 6
        return preg_split('/(\s)?\.(\s)?/', $parameter, null, PREG_SPLIT_NO_EMPTY);
147
148
    }
149
150
}
151