Passed
Push — master ( 9d6707...96b306 )
by Andreas
17:54
created

images::save()   B

Complexity

Conditions 11
Paths 91

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.6363

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 22
c 1
b 0
f 0
nc 91
nop 0
dl 0
loc 34
ccs 19
cts 23
cp 0.8261
crap 11.6363
rs 7.3166

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\storage;
7
8
use midcom\datamanager\helper\imagefilter;
9
10
/**
11
 * Images storage
12
 *
13
 * Controls a list of images
14
 */
15
class images extends blobs implements recreateable
16
{
17
    public function recreate() : bool
18
    {
19
        $this->map = [];
20
        $map = $this->load();
21
22
        foreach ($map as $identifier => &$images) {
23
            $filter = new imagefilter($this->config['type_config']);
24
            $images = $filter->process($images['main'], $images);
25
26
            foreach ($images as $name => $image) {
27
                $this->map[$identifier . $name] = $image;
28
            }
29
        }
30
        return $this->save_image_map($map) && $this->save_attachment_list();
31
    }
32
33 4
    public function load()
34
    {
35 4
        $results = parent::load();
36 4
        $grouped = [];
37
38 4
        $identifiers = [];
39 4
        if ($raw_list = $this->object->get_parameter('midcom.helper.datamanager2.type.images', "attachment_map_{$this->config['name']}")) {
40 2
            $identifiers = explode(',', $raw_list);
41
        } else {
42
            // Reconstruct from blobs data
43 2
            foreach (array_keys($results) as $identifier) {
44
                $identifiers[] = $identifier . ':' . substr($identifier, 0, 32) . ':main';
45
            }
46
        }
47
48 4
        $map = [];
49 4
        foreach ($identifiers as $item) {
50 2
            [$identifier, $images_identifier, $images_name] = explode(':', $item);
51 2
            $map[$identifier] = [$images_identifier, $images_name];
52
        }
53
        // we iterate over results since that takes sorting into account
54 4
        foreach ($results as $identifier => $image) {
55 2
            [$images_identifier, $images_name] = $map[$identifier];
56 2
            if (!array_key_exists($images_identifier, $grouped)) {
57 2
                $grouped[$images_identifier] = [];
58
            }
59 2
            $grouped[$images_identifier][$images_name] = $image;
60
        }
61
62 4
        return $grouped;
63
    }
64
65 1
    public function save()
66
    {
67 1
        $this->map = [];
68 1
        $map = $this->load();
69 1
        foreach ($this->value as $identifier => $images) {
70 1
            if (!empty($images['file'])) {
71 1
                if (is_numeric($identifier)) {
72 1
                    $identifier = md5(time() . $images['file']->name . $images['file']->location);
73
                }
74 1
                $images['file']->parentguid = $this->object->guid;
75 1
                $existing = array_key_exists($identifier, $map) ? $map[$identifier] : [];
76 1
                $filter = new imagefilter($this->config['type_config']);
77 1
                $map[$identifier] = $filter->process($images['file'], $existing);
78
            }
79 1
            foreach ($map[$identifier] as $name => $image) {
80 1
                $this->map[$identifier . $name] = $image;
81
            }
82
83 1
            if (!empty($images['main'])) {
84 1
                if (array_key_exists('description', $images)) {
85
                    $images['main']->set_parameter('midcom.helper.datamanager2.type.blobs', 'description', $images['description']);
86
                }
87 1
                if (array_key_exists('title', $images) && $images['main']->title != $images['title']) {
88
                    $images['main']->title = $images['title'];
89
                    $images['main']->update();
90
                }
91 1
                if (!empty($this->config['widget_config']['sortable'])) {
92
                    $images['main']->update();
93
                }
94
            }
95
        }
96
97 1
        $this->save_image_map($map);
98 1
        $this->save_attachment_list();
99 1
    }
100
101 1
    private function save_image_map(array $map) : bool
102
    {
103 1
        $list = [];
104
105 1
        foreach ($map as $identifier => $derived) {
106 1
            foreach (array_keys($derived) as $name) {
107 1
                $list[] = $identifier . $name . ':' . $identifier . ':' . $name;
108
            }
109
        }
110
111 1
        return $this->object->set_parameter('midcom.helper.datamanager2.type.images', "attachment_map_{$this->config['name']}", implode(',', $list));
112
    }
113
}
114