Completed
Push — master ( 3a258b...3917d8 )
by Andreas
13:50
created

imageTransformer::reverseTransform()   B

Complexity

Conditions 10
Paths 25

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.5498

Importance

Changes 0
Metric Value
cc 10
eloc 16
nc 25
nop 1
dl 0
loc 24
ccs 14
cts 17
cp 0.8235
crap 10.5498
rs 7.6666
c 0
b 0
f 0

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
/**
9
 * Image transformer
10
 *
11
 * This handles multiple attachments because of filter chains and such
12
 */
13
class imageTransformer extends attachmentTransformer
14
{
15 25
    public function transform($input)
16
    {
17 25
        if ($input === null) {
18 24
            return [];
19
        }
20
21 22
        $result = ['objects' => []];
22
23 22
        foreach ($input as $key => $value) {
24 1
            if ($key === 'delete' || $key === 'description' || $key == 'title') {
25 1
                $result[$key] = $value;
26
            } else {
27 1
                $result['objects'][$key] = parent::transform($value);
28 1
                if ($key === 'file') {
29
                    $result['identifier'] = $result['objects'][$key]['identifier'];
30
                }
31 1
                if ($key === 'main') {
32 1
                    if (!empty($this->config['widget_config']['show_title'])) {
33 1
                        $result['title'] = $result['objects'][$key]['title'];
34
                    }
35 1
                    if (!empty($this->config['widget_config']['show_description'])) {
36 1
                        $result['description'] = $result['objects'][$key]['description'];
37
                    }
38
                }
39
            }
40
        }
41 22
        return $result;
42
    }
43
44 4
    public function reverseTransform($array)
45
    {
46 4
        if (empty($array)) {
47 1
            return null;
48
        }
49 3
        $result = [];
50 3
        if (   !empty($array['file'])
51 3
            || !empty($array['identifier']) && substr($array['identifier'], 0, 8) === 'tmpfile-') {
52
            $result['file'] = parent::reverseTransform($array);
53 3
        } elseif (!empty($array['objects'])) {
54 1
            foreach ($array['objects'] as $key => $value) {
55 1
                $result[$key] = parent::reverseTransform($value);
56
            }
57
        }
58 3
        if (!empty($array['delete'])) {
59
            $result['delete'] = $array['delete'];
60
        }
61 3
        if (!empty($this->config['widget_config']['show_description'])) {
62
            $result['description'] = $array['description'];
63
        }
64 3
        if (!empty($this->config['widget_config']['show_title'])) {
65 1
            $result['title'] = $array['title'];
66
        }
67 3
        return $result;
68
    }
69
}
70