Test Setup Failed
Pull Request — master (#190)
by
unknown
09:44
created

parameter::convert_multiple_from_storage()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 9
nop 1
dl 0
loc 29
ccs 0
cts 0
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\storage;
7
8
/**
9
 * Experimental storage class
10
 */
11
class parameter extends delayed
12
{
13
    private $multiple_separator = '|';
14
15
    /**
16
     * This member contains the other key, in case it is set. In case of multiselects,
17
     * the full list of unknown keys is collected here, in case of single select, this value
18
     * takes precedence from the standard selection.
19
     *
20
     * This is only valid if the allow_other flag is set.
21
     * TODO: still to be implentend
22
     * @var string
23
     */
24 2
    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...
25
26
    /**
27
     * {@inheritdoc}
28 2
     */
29 2
    public function load()
30
    {
31
        $source = $this->object->get_parameter($this->config['storage']['domain'], $this->config['storage']['name']);
32 2
33
        if (!empty($this->config['type_config']['multiple_storagemode'])) {
34
            $source = $this->convert_multiple_from_storage($source);
35
        }
36
37
        return $source;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function save()
44
    {
45
        //workaround for weird mgd API behavior where setting falsy (i.e. deleting) a nonexistent parameter
46
        //returns an error
47
        if (   !$this->value
48
            && $this->load() === null) {
49
            return true;
50
        }
51
52
        if (!empty($this->config['type_config']['multiple_storagemode'])) {
53
            $this->value = $this->convert_multiple_to_storage();
54
        }
55
56
        return $this->object->set_parameter($this->config['storage']['domain'], $this->config['storage']['name'], $this->value);
57
    }
58
59
    private function convert_multiple_from_storage($source)
60
    {
61
        $glue = $this->multiple_separator;
62
63
        switch ($this->config['type_config']['multiple_storagemode']) {
64
            case 'serialized':
65
            case 'array':
66
                if (   !is_array($source)
67
                    && empty($source)) {
68
                    $source = array();
69
                }
70
                return $source;
71
72
            case 'imploded':
73
                if (!is_string($source)) {
74
                    return array();
75
                }
76
                return explode($glue, $source);
77
78
            case 'imploded_wrapped':
79
                if (!is_string($source)) {
80
                    return array();
81
                }
82
                return explode($glue, substr($source, 1, -1));
83
84
            default:
85
                throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue.");
86
        }
87
    }
88
89
    /**
90
     * Converts the selected options according to the multiple_storagemode setting.
91
     *
92
     * @return mixed The data converted to the final data storage.
93
    */
94
    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...
95
    {
96
        switch ($this->config['type_config']['multiple_storagemode']) {
97
            case 'array':
98
                return $this->value;
99
100
            case 'serialized':
101
                if ($this->others) {
102
                    return array_merge($this->value, $this->others);
103
                }
104
                return $this->value;
105
106
            case 'imploded':
107
                $options = $this->get_imploded_options();
108
                return $options;
109
110
            case 'imploded_wrapped':
111
                $glue = $this->multiple_separator;
112
                $options = $this->get_imploded_options();
113
                return "{$glue}{$options}{$glue}";
114
115
            default:
116
                throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue.");
117
        }
118
    }
119
120
    /**
121
     * Prepares the imploded storage string. All entries containing the pipe char (used as glue)
122
     * will be logged and skipped silently.
123
     *
124
     * @return string The imploded data string.
125
     */
126
    private function get_imploded_options()
127
    {
128
        $glue = $this->multiple_separator;
129
130
        if ($this->others) {
131
            if (is_string($this->others)) {
132
                $this->others = array(
133
                    $this->others => $this->others,
134
                );
135
            }
136
            $options = array_merge($this->value, $this->others);
137
        } else {
138
            $options = $this->value;
139
        }
140
141
        $result = array();
142
        foreach ($options as $key) {
143
            if (strpos($key, $glue) !== false) {
144
                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.",
145
                MIDCOM_LOG_WARN);
146
                continue;
147
            }
148
149
            $result[] = $key;
150
        }
151
        return implode($glue, $result);
152
    }
153
}