Passed
Push — master ( 625fac...5ee9cd )
by Andreas
25:03 queued 06:55
created

images::save()   B

Complexity

Conditions 10
Paths 55

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10.6008

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 2
b 0
f 0
nc 55
nop 0
dl 0
loc 33
ccs 18
cts 22
cp 0.8182
crap 10.6008
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\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
                $filter = new imagefilter($this->config['type_config']);
76 1
                $map[$identifier] = $filter->process($images['file'], $map[$identifier] ?? []);
77
            }
78 1
            foreach ($map[$identifier] as $name => $image) {
79 1
                $this->map[$identifier . $name] = $image;
80
            }
81
82 1
            if (!empty($images['main'])) {
83 1
                if (array_key_exists('description', $images)) {
84
                    $images['main']->set_parameter('midcom.helper.datamanager2.type.blobs', 'description', $images['description']);
85
                }
86 1
                if (array_key_exists('title', $images) && $images['main']->title != $images['title']) {
87
                    $images['main']->title = $images['title'];
88
                    $images['main']->update();
89
                }
90 1
                if (!empty($this->config['widget_config']['sortable'])) {
91
                    $images['main']->update();
92
                }
93
            }
94
        }
95
96 1
        $this->save_image_map($map);
97 1
        $this->save_attachment_list();
98 1
    }
99
100 1
    private function save_image_map(array $map) : bool
101
    {
102 1
        $list = [];
103
104 1
        foreach ($map as $identifier => $derived) {
105 1
            foreach (array_keys($derived) as $name) {
106 1
                $list[] = $identifier . $name . ':' . $identifier . ':' . $name;
107
            }
108
        }
109
110 1
        return $this->object->set_parameter('midcom.helper.datamanager2.type.images', "attachment_map_{$this->config['name']}", implode(',', $list));
111
    }
112
}
113