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

images::load()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.0061

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 12
nop 0
dl 0
loc 30
ccs 17
cts 18
cp 0.9444
crap 6.0061
rs 9.0444
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\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