Failed Conditions
Push — psr2-config ( e063ba...8ea568 )
by Andreas
03:29
created

ConfigSettings   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getSettings() 0 3 1
A getUndefined() 0 3 1
A getErrors() 0 3 1
C initSettings() 0 23 7
A instantiateClass() 0 12 2
A determineClassName() 0 14 4
1
<?php
2
3
namespace dokuwiki\plugin\config\core;
4
5
/**
6
 * Holds all the current settings
7
 */
8
class ConfigSettings {
9
10
    /** @var Setting[] metadata as array of Settings objects */
11
    protected $settings = array();
12
    /** @var array problematic keys */
13
    protected $errors;
14
    /** @var Setting undefined settings */
15
    protected $undefined = array();
16
17
    /** @var array all metadata */
18
    protected $metadata;
19
    /** @var array all default settings */
20
    protected $default;
21
    /** @var array all local settings */
22
    protected $local;
23
    /** @var array all protected settings */
24
    protected $protected;
25
26
    /**
27
     * ConfigSettings constructor.
28
     */
29
    public function __construct() {
30
        $loader = new Loader(new ConfigParser());
31
32
        $this->metadata = $loader->loadMeta();
33
        $this->default = $loader->loadDefaults();
34
        $this->local = $loader->loadLocal();
35
        $this->protected = $loader->loadProtected();
36
37
        $this->initSettings();
38
    }
39
40
    /**
41
     * Get all settings
42
     *
43
     * @return Setting[]
44
     */
45
    public function getSettings() {
46
        return $this->settings;
47
    }
48
49
    /**
50
     * Get all unknown settings
51
     *
52
     * @return Setting
53
     */
54
    public function getUndefined() {
55
        return $this->undefined;
56
    }
57
58
    /**
59
     * Get the settings that had some kind of setup problem
60
     *
61
     * @return array associative error, key is the setting, value the error
62
     */
63
    public function getErrors() {
64
        return $this->errors;
65
    }
66
67
    /**
68
     * Initalizes the $settings and $undefined properties
69
     */
70
    protected function initSettings() {
71
        $keys = array_merge(
72
            array_keys($this->metadata),
73
            array_keys($this->default),
74
            array_keys($this->local),
75
            array_keys($this->protected)
76
        );
77
        $keys = array_unique($keys);
78
79
        foreach($keys as $key) {
80
            $obj = $this->instantiateClass($key);
81
82
            if($obj->shouldHaveDefault() && !isset($this->default[$key])) {
83
                $this->errors[$key] = 'no default';
84
            }
85
86
            $d = isset($this->default[$key]) ? $this->default[$key] : null;
87
            $l = isset($this->local[$key]) ? $this->local[$key] : null;
88
            $p = isset($this->protected[$key]) ? $this->protected[$key] : null;
89
90
            $obj->initialize($d, $l, $p);
91
        }
92
    }
93
94
    /**
95
     * Instantiates the proper class for the given config key
96
     *
97
     * The class is added to the $settings or $undefined arrays and returned
98
     *
99
     * @param string $key
100
     * @return Setting
101
     */
102
    protected function instantiateClass($key) {
103
        if(isset($this->metadata[$key])) {
104
            $param = $this->metadata[$key];
105
            $class = $this->determineClassName(array_shift($param), $key); // first param is class
106
            $obj = new $class($key, $param);
107
            $this->settings[] = $obj;
108
        } else {
109
            $obj = new SettingUndefined($key);
110
            $this->undefined[] = $obj;
111
        }
112
        return $obj;
113
    }
114
115
    /**
116
     * Return the class to load
117
     *
118
     * @param string $class the class name as given in the meta file
119
     * @param string $key the settings key
120
     * @return string
121
     */
122
    protected function determineClassName($class, $key) {
123
        // try namespaced class first
124
        if($class) {
125
            $modern = str_replace('_', '', ucwords($class, '_'));
126
            $modern = '\\dokuwiki\\plugin\\config\\core\\' . $modern;
127
            if($modern && class_exists($modern)) return $modern;
128
            // class wasn't found add to errors
129
            $this->errors[$key] = 'unknown class';
130
        } else {
131
            // no class given, add to errors
132
            $this->errors[$key] = 'no class';
133
        }
134
        return '\\dokuwiki\\plugin\\config\\core\\Setting';
135
    }
136
137
}
138