Test Setup Failed
Pull Request — master (#190)
by
unknown
08:43
created

parameter   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
dl 0
loc 143
ccs 4
cts 7
cp 0.5714
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 10 2
A save() 0 15 4
D convert_multiple_from_storage() 0 29 9
B convert_multiple_to_storage() 0 25 6
B get_imploded_options() 0 27 5
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\storage;
7
8
use midcom_error;
9
10
/**
11
 * Experimental storage class
12
 */
13
class parameter extends delayed
14
{
15
    private $multiple_separator = '|';
16
17
    /**
18
     * This member contains the other key, in case it is set. In case of multiselects,
19
     * the full list of unknown keys is collected here, in case of single select, this value
20
     * takes precedence from the standard selection.
21
     *
22
     * This is only valid if the allow_other flag is set.
23
     * TODO: still to be implentend
24 2
     * @var string
25
     */
26
    private $other = array();
0 ignored issues
show
Unused Code introduced by
The property $other is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
28 2
    /**
29 2
     * {@inheritdoc}
30
     */
31
    public function load()
32 2
    {
33
        $source = $this->object->get_parameter($this->config['storage']['domain'], $this->config['storage']['name']);
34
35
        if (!empty($this->config['type_config']['multiple_storagemode'])) {
36
            $source = $this->convert_multiple_from_storage($source);
37
        }
38
39
        return $source;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function save()
46
    {
47
        //workaround for weird mgd API behavior where setting falsy (i.e. deleting) a nonexistent parameter
48
        //returns an error
49
        if (   !$this->value
50
            && $this->load() === null) {
51
            return true;
52
        }
53
54
        if (!empty($this->config['type_config']['multiple_storagemode'])) {
55
            $this->value = $this->convert_multiple_to_storage();
56
        }
57
58
        return $this->object->set_parameter($this->config['storage']['domain'], $this->config['storage']['name'], $this->value);
59
    }
60
61
    private function convert_multiple_from_storage($source)
62
    {
63
        $glue = $this->multiple_separator;
64
65
        switch ($this->config['type_config']['multiple_storagemode']) {
66
            case 'serialized':
67
            case 'array':
68
                if (   !is_array($source)
69
                    && empty($source)) {
70
                    $source = array();
71
                }
72
                return $source;
73
74
            case 'imploded':
75
                if (!is_string($source)) {
76
                    return array();
77
                }
78
                return explode($glue, $source);
79
80
            case 'imploded_wrapped':
81
                if (!is_string($source)) {
82
                    return array();
83
                }
84
                return explode($glue, substr($source, 1, -1));
85
86
            default:
87
                throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue.");
88
        }
89
    }
90
91
    /**
92
     * Converts the selected options according to the multiple_storagemode setting.
93
     *
94
     * @return mixed The data converted to the final data storage.
95
    */
96
    function convert_multiple_to_storage()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
    {
98
        switch ($this->config['type_config']['multiple_storagemode']) {
99
            case 'array':
100
                return $this->value;
101
102
            case 'serialized':
103
                if ($this->others) {
104
                    return array_merge($this->value, $this->others);
105
                }
106
                return $this->value;
107
108
            case 'imploded':
109
                $options = $this->get_imploded_options();
110
                return $options;
111
112
            case 'imploded_wrapped':
113
                $glue = $this->multiple_separator;
114
                $options = $this->get_imploded_options();
115
                return "{$glue}{$options}{$glue}";
116
117
            default:
118
                throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue.");
119
        }
120
    }
121
122
    /**
123
     * Prepares the imploded storage string. All entries containing the pipe char (used as glue)
124
     * will be logged and skipped silently.
125
     *
126
     * @return string The imploded data string.
127
     */
128
    private function get_imploded_options()
129
    {
130
        $glue = $this->multiple_separator;
131
132
        if ($this->others) {
133
            if (is_string($this->others)) {
134
                $this->others = array(
135
                    $this->others => $this->others,
136
                );
137
            }
138
            $options = array_merge($this->value, $this->others);
139
        } else {
140
            $options = $this->value;
141
        }
142
143
        $result = array();
144
        foreach ($options as $key) {
145
            if (strpos($key, $glue) !== false) {
146
                debug_add("The option key '{$key}' contained the multiple separator ($this->config['type_config']['multiple_storagemode']) char, which is not allowed for imploded storage targets. ignoring silently.",
147
                MIDCOM_LOG_WARN);
148
                continue;
149
            }
150
151
            $result[] = $key;
152
        }
153
        return implode($glue, $result);
154
    }
155
}