Test Setup Failed
Pull Request — master (#190)
by
unknown
15:14
created

parameter   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 57.14%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 9 2
A save() 0 14 4
D convert_multiple_from_storage() 0 29 9
B convert_multiple_to_storage() 0 25 6
B getImplodedOptions() 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
/**
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
     * {@inheritdoc}
27
     */
28 2
    public function load()
29 2
    {
30
        $source = $this->object->get_parameter($this->config['storage']['domain'], $this->config['storage']['name']);
31
        if(isset($this->config['type_config']['multiple_storagemode'])) {
32 2
            $source = $this->convert_multiple_from_storage($source);
33
        }
34
35
        return $source;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function save()
42
    {
43
        //workaround for weird mgd API behavior where setting falsy (i.e. deleting) a nonexistent parameter
44
        //returns an error
45
        if (   !$this->value
46
            && $this->load() === null) {
47
            return true;
48
        }
49
        if(isset($this->config['type_config']['multiple_storagemode'])) {
50
            $this->value = $this->convert_multiple_to_storage();
51
        }
52
53
        return $this->object->set_parameter($this->config['storage']['domain'], $this->config['storage']['name'], $this->value);
54
    }
55
56
    private function convert_multiple_from_storage($source)
57
    {
58
        $glue = $this->multiple_separator;
59
60
        switch ($this->config['type_config']['multiple_storagemode']) {
61
            case 'serialized':
62
            case 'array':
63
                if (   !is_array($source)
64
                    && empty($source)) {
65
                    $source = array();
66
                }
67
                return $source;
68
69
            case 'imploded':
70
                if (!is_string($source)) {
71
                    return array();
72
                }
73
                return explode($glue, $source);
74
75
            case 'imploded_wrapped':
76
                if (!is_string($source)) {
77
                    return array();
78
                }
79
                return explode($glue, substr($source, 1, -1));
80
81
            default:
82
                throw new midcom_error("The multiple_storagemode '{$this->multiple_storagemode}' is invalid, cannot continue.");
83
        }
84
    }
85
86
    /**
87
     * Converts the selected options according to the multiple_storagemode setting.
88
     *
89
     * @return mixed The data converted to the final data storage.
90
    */
91
    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...
92
    {
93
        switch ($this->config['type_config']['multiple_storagemode']) {
94
            case 'array':
95
                return $this->value;
96
97
            case 'serialized':
98
                if ($this->others) {
99
                    return array_merge($this->value, $this->others);
100
                }
101
                return $this->value;
102
103
            case 'imploded':
104
                $options = $this->getImplodedOptions();
105
                return $options;
106
107
            case 'imploded_wrapped':
108
                $glue = $this->multiple_separator;
109
                $options = $this->getImplodedOptions();
110
                return "{$glue}{$options}{$glue}";
111
112
            default:
113
                throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue.");
114
        }
115
    }
116
117
    /**
118
     * Prepares the imploded storage string. All entries containing the pipe char (used as glue)
119
     * will be logged and skipped silently.
120
     *
121
     * @return string The imploded data string.
122
     */
123
    private function getImplodedOptions()
124
    {
125
        $glue = $this->multiple_separator;
126
127
        if ($this->others) {
128
            if (is_string($this->others)) {
129
                $this->others = array(
130
                    $this->others => $this->others,
131
                );
132
            }
133
            $options = array_merge($this->value, $this->others);
134
        } else {
135
            $options = $this->value;
136
        }
137
138
        $result = array();
139
        foreach ($options as $key) {
140
            if (strpos($key, $glue) !== false) {
141
                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.",
142
                MIDCOM_LOG_WARN);
143
                continue;
144
            }
145
146
            $result[] = $key;
147
        }
148
        return implode($glue, $result);
149
    }
150
}