Completed
Push — master ( 088fe8...421f0e )
by Andreas
22:30
created

imageTransformer::transform()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.125

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 13
nop 1
dl 0
loc 26
ccs 14
cts 16
cp 0.875
crap 8.125
rs 8.4444
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\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 22
        $result = ['objects' => []];
21
22 22
        foreach ($input as $key => $value) {
23 1
            if (in_array($key, ['delete', 'description', 'title', 'score'])) {
24 1
                $result[$key] = $value;
25
            } else {
26 1
                $result['objects'][$key] = parent::transform($value);
27 1
                if ($key === 'file') {
28
                    $result['identifier'] = $result['objects'][$key]['identifier'];
29
                }
30 1
                if ($key === 'main') {
31 1
                    if (!empty($this->config['widget_config']['show_title'])) {
32 1
                        $result['title'] = $result['objects'][$key]['title'];
33
                    }
34 1
                    if (!empty($this->config['widget_config']['show_description'])) {
35
                        $result['description'] = $result['objects'][$key]['description'];
36
                    }
37
                }
38
            }
39
        }
40 22
        return $result;
41
    }
42
43 4
    public function reverseTransform($array)
44
    {
45 4
        if (empty($array)) {
46 1
            return null;
47
        }
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
                if (array_key_exists('score', $array)) {
56
                    $value['score'] = $array['score'];
57
                }
58 1
                $result[$key] = parent::reverseTransform($value);
59
            }
60
        }
61 3
        if (!empty($array['delete'])) {
62
            $result['delete'] = $array['delete'];
63
        }
64 3
        if (!empty($this->config['widget_config']['show_description'])) {
65
            $result['description'] = $array['description'];
66
        }
67 3
        if (!empty($this->config['widget_config']['show_title'])) {
68 1
            $result['title'] = $array['title'];
69
        }
70 3
        if (!empty($this->config['widget_config']['sortable'])) {
71
            $result['score'] = $array['score'];
72
        }
73 3
        return $result;
74
    }
75
}
76