Test Setup Failed
Pull Request — master (#190)
by
unknown
17:12
created

parameter::get_imploded_options()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 9
nop 0
dl 0
loc 27
ccs 0
cts 0
cp 0
crap 30
rs 8.439
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
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
        $value = $this->value;
55
        if (!empty($this->config['type_config']['multiple_storagemode'])) {
56
            $value = $this->convert_multiple_to_storage();
57
        }
58
59
        return $this->object->set_parameter($this->config['storage']['domain'], $this->config['storage']['name'], $value);
60
    }
61
62
    private function convert_multiple_from_storage($source)
63
    {
64
        $glue = $this->multiple_separator;
65
66
        switch ($this->config['type_config']['multiple_storagemode']) {
67
            case 'serialized':
68
            case 'array':
69
                if (   !is_array($source)
70
                    && empty($source)) {
71
                    $source = array();
72
                }
73
                return $source;
74
75
            case 'imploded':
76
                if (!is_string($source)) {
77
                    return array();
78
                }
79
                return explode($glue, $source);
80
81
            case 'imploded_wrapped':
82
                if (!is_string($source)) {
83
                    return array();
84
                }
85
                return explode($glue, substr($source, 1, -1));
86
87
            default:
88
                throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue.");
89
        }
90
    }
91
92
    /**
93
     * Converts the selected options according to the multiple_storagemode setting.
94
     *
95
     * @return mixed The data converted to the final data storage.
96
     */
97
    private function convert_multiple_to_storage()
98
    {
99
        switch ($this->config['type_config']['multiple_storagemode']) {
100
            case 'array':
101
                return $this->value;
102
103
            case 'serialized':
104
                if ($this->others) {
105
                    return array_merge($this->value, $this->others);
106
                }
107
                return $this->value;
108
109
            case 'imploded':
110
                $options = $this->get_imploded_options();
111
                return $options;
112
113
            case 'imploded_wrapped':
114
                $glue = $this->multiple_separator;
115
                $options = $this->get_imploded_options();
116
                return "{$glue}{$options}{$glue}";
117
118
            default:
119
                throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue.");
120
        }
121
    }
122
123
    /**
124
     * Prepares the imploded storage string. All entries containing the pipe char (used as glue)
125
     * will be logged and skipped silently.
126
     *
127
     * @return string The imploded data string.
128
     */
129
    private function get_imploded_options()
130
    {
131
        $glue = $this->multiple_separator;
132
133
        if ($this->others) {
134
            if (is_string($this->others)) {
135
                $this->others = array(
136
                    $this->others => $this->others,
137
                );
138
            }
139
            $options = array_merge($this->value, $this->others);
140
        } else {
141
            $options = $this->value;
142
        }
143
144
        $result = array();
145
        foreach ($options as $key) {
146
            if (strpos($key, $glue) !== false) {
147
                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.",
148
                MIDCOM_LOG_WARN);
149
                continue;
150
            }
151
152
            $result[] = $key;
153
        }
154
        return implode($glue, $result);
155
    }
156
}