Completed
Push — 4.0 ( 2ca4e1...0c3d7c )
by Marco
15:24
created

Configuration::get()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 41
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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