Completed
Push — 4.0 ( 0161d2...25fc97 )
by Marco
12:04
created

Configuration::loadFromYaml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
/**
4
 * @package     Comodojo Dispatcher
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @author      Marco Castiello <[email protected]>
7
 * @license     GPL-3.0+
8
 *
9
 * LICENSE:
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
26
class Configuration {
27
28
    protected $attributes = array();
29
30
    public function __construct( $configuration = array() ) {
31
32
        $this->attributes = array_merge($this->attributes, $configuration);
33
34
    }
35
36
    final public function get($property = null) {
37
38
        if ( is_null($property) ) {
39
40
            return $this->attributes;
41
42
        } else if (array_key_exists($property, $this->attributes)) {
43
44
            $value = $this->attributes[$property];
45
46
            // substitution by backreference is cool but hard compute for a large set of values :(
47
            //
48
            // if ( is_scalar($value) && preg_match_all('/%(.+?)%/', $value, $matches, PREG_SET_ORDER) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
49
            //
50
            //     $substitutions = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
51
            //
52
            //     foreach ( $matches as $match ) {
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...
53
            //
54
            //         $backreference = $match[1];
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...
55
            //
56
            //         if ( $backreference != $property && !isset($substitutions['/%'.$backreference.'%/']) ) {
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...
57
            //
58
            //             $substitutions['/%'.$backreference.'%/'] = $this->get($backreference);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
59
            //
60
            //         }
61
            //
62
            //     }
63
            //
64
            //     $value = preg_replace(array_keys($substitutions), array_values($substitutions), $value);
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...
65
            //
66
            // }
67
68
            return $value;
69
70
        } else {
71
72
            return null;
73
74
        }
75
76
    }
77
78
    final public function set($property, $value) {
79
80
        $this->attributes[$property] = $value;
81
82
        return $this;
83
84
    }
85
86
    final public function isDefined($property) {
87
88
        return isset($this->attributes[$property]);
89
90
    }
91
92
    final public function delete($property = null) {
93
94
        if ( is_null($property) ) {
95
96
            $this->attributes = array();
97
98
            return true;
99
100
        } else if ( $this->isDefined($property) ) {
101
102
            unset($this->attributes[$property]);
103
104
            return true;
105
106
        } else {
107
108
            return false;
109
110
        }
111
112
    }
113
114
    final public function merge($properties) {
115
116
        $this->attributes = array_replace($this->attributes, $properties);
117
118
        return $this;
119
120
    }
121
122
}
123