Passed
Push — master ( f5c71d...de40ed )
by Andreas
39:42
created

multipleTransformer::transform()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11.097

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 30
ccs 14
cts 18
cp 0.7778
crap 11.097
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\extension\transformer;
7
8
use Symfony\Component\Form\DataTransformerInterface;
9
use Symfony\Component\Form\Exception\TransformationFailedException;
10
use midcom_error;
11
12
/**
13
 * Experimental jsdate transformer
14
 */
15
class multipleTransformer implements DataTransformerInterface
16
{
17
    private $config;
18
19
    private $multiple_separator = '|';
20
21
    private $multiple_storagemode = 'serialized';
22
23 33
    public function __construct(array $config)
24
    {
25 33
        $this->config = $config;
26 33
        if (!empty($this->config['type_config']['multiple_storagemode'])) {
27 32
            $this->multiple_storagemode = $this->config['type_config']['multiple_storagemode'];
28
        }
29 33
    }
30
31 29
    public function transform($input)
32
    {
33 29
        if (in_array($input, [false, null], true)) {
34 26
            return [];
35
        }
36
37 18
        switch ($this->multiple_storagemode) {
38 18
            case 'serialized':
39 14
            case 'array':
40 4
                if (empty($input)) {
41 1
                    return [];
42
                }
43
44 3
                return unserialize($input);
45
46 14
            case 'imploded':
47
                if (!is_string($input)) {
48
                    return [];
49
                }
50
                return explode($this->multiple_separator, $input);
51
52 14
            case 'imploded_wrapped':
53 14
                if (!is_string($input) || substr($input, 1, -1) == '') {
54 13
                    return [];
55
                }
56
57 1
                return explode($this->multiple_separator, substr($input, 1, -1));
58
59
            default:
60
                throw new midcom_error("The multiple_storagemode '{$this->multiple_storagemode}' is invalid, cannot continue.");
61
        }
62
    }
63
64 5
    public function reverseTransform($array)
65
    {
66 5
        if (!is_array($array) ) {
67
            throw new TransformationFailedException('Expected an array.');
68
        }
69
70 5
        if (empty($array)) {
71 3
            return;
72
        }
73
74 2
        switch ($this->multiple_storagemode) {
75 2
            case 'array':
76
                return $array;
77
78 2
            case 'serialized':
79 1
                return serialize($array);
80
81 1
            case 'imploded':
82
                return $this->get_imploded_options($array);
83
84 1
            case 'imploded_wrapped':
85 1
                $glue = $this->multiple_separator;
86 1
                $options = $this->get_imploded_options($array);
87 1
                return "{$glue}{$options}{$glue}";
88
89
            default:
90
                throw new TransformationFailedException('Invalid storage mode ' . $this->multiple_storagemode);
91
        }
92
    }
93
94
    /**
95
     * Prepares the imploded storage string. All entries containing the pipe char (used as glue)
96
     * will be logged and skipped silently.
97
     */
98 1
    private function get_imploded_options(array $array) : string
99
    {
100 1
        $glue = $this->multiple_separator;
101
102 1
        $result = [];
103 1
        foreach ($array as $key) {
104 1
            if (str_contains($key, $glue)) {
105
                debug_add("The option key '{$key}' contained the multiple separator ($this->multiple_storagemode) char, which is not allowed for imploded storage targets. ignoring silently.",
106
                MIDCOM_LOG_WARN);
107
                continue;
108
            }
109
110 1
            $result[] = $key;
111
        }
112 1
        return implode($glue, $result);
113
    }
114
}
115