Failed Conditions
Pull Request — psr2 (#2382)
by Andreas
08:02 queued 12s
created

SettingOnoff   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 15
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A html() 0 18 4
B update() 0 10 5
B cleanValue() 0 11 6
1
<?php
2
3
namespace dokuwiki\plugin\config\core\Setting;
4
5
/**
6
 * Class setting_onoff
7
 */
8
class SettingOnoff extends SettingNumeric {
9
10
    /**
11
     * We treat the strings 'false' and 'off' as false
12
     * @inheritdoc
13
     */
14
    protected function cleanValue($value) {
15
        if($value === null) return null;
16
17
        if(is_string($value)) {
18
            if(strtolower($value) === 'false') return 0;
19
            if(strtolower($value) === 'off') return 0;
20
            if(trim($value) === '') return 0;
21
        }
22
23
        return (int) (bool) $value;
24
    }
25
26
    /** @inheritdoc */
27
    public function html(\admin_plugin_config $plugin, $echo = false) {
28
        $disable = '';
29
30
        if($this->isProtected()) {
31
            $value = $this->protected;
32
            $disable = ' disabled="disabled"';
33
        } else {
34
            $value = is_null($this->local) ? $this->default : $this->local;
35
        }
36
37
        $key = htmlspecialchars($this->key);
38
        $checked = ($value) ? ' checked="checked"' : '';
39
40
        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
41
        $input = '<div class="input"><input id="config___' . $key . '" name="config[' . $key .
42
            ']" type="checkbox" class="checkbox" value="1"' . $checked . $disable . '/></div>';
43
        return array($label, $input);
44
    }
45
46
    /** @inheritdoc */
47
    public function update($input) {
48
        if($this->isProtected()) return false;
49
50
        $input = ($input) ? 1 : 0;
51
        $value = is_null($this->local) ? $this->default : $this->local;
52
        if($value == $input) return false;
53
54
        $this->local = $input;
55
        return true;
56
    }
57
}
58