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

images   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 91
ccs 39
cts 51
cp 0.7647
rs 10
c 0
b 0
f 0
wmc 25

4 Methods

Rating   Name   Duplication   Size   Complexity  
A recreate() 0 14 4
A load() 0 27 6
C save() 0 31 12
A save_image_map() 0 11 3
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()
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->guid] = $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
        foreach ($identifiers as $item) {
49 2
            list($identifier, $images_identifier, $images_name) = explode(':', $item);
50
51 2
            if (array_key_exists($identifier, $results)) {
52 2
                if (!array_key_exists($images_identifier, $grouped)) {
53 2
                    $grouped[$images_identifier] = [];
54
                }
55 2
                $grouped[$images_identifier][$images_name] = $results[$identifier];
56
            }
57
        }
58
59 4
        return $grouped;
60
    }
61
62 1
    public function save()
63
    {
64 1
        $this->map = [];
65 1
        $map = $this->load();
66 1
        foreach ($this->value as $identifier => $images) {
67 1
            if (!empty($images['file'])) {
68 1
                if (is_numeric($identifier)) {
69 1
                    $identifier = md5(time() . $images['file']->name . $images['file']->location);
70
                }
71 1
                $images['file']->parentguid = $this->object->guid;
72 1
                $existing = array_key_exists($identifier, $map) ? $map[$identifier] : [];
73 1
                $filter = new imagefilter($this->config['type_config']);
74 1
                $map[$identifier] = $filter->process($images['file'], $existing);
75
            }
76 1
            if (!empty($map[$identifier])) {
77 1
                foreach ($map[$identifier] as $name => $image) {
78 1
                    $this->map[$identifier . $name . ':' . $image->guid] = $image;
79
                }
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 1
                    $images['main']->update();
89
                }
90
            }
91
        }
92 1
        return $this->save_image_map($map) && $this->save_attachment_list();
93
    }
94
95 1
    private function save_image_map(array $map)
96
    {
97 1
        $list = [];
98
99 1
        foreach ($map as $identifier => $derived) {
100 1
            foreach (array_keys($derived) as $name) {
101 1
                $list[] = $identifier . $name . ':' . $identifier . ':' . $name;
102
            }
103
        }
104
105 1
        return $this->object->set_parameter('midcom.helper.datamanager2.type.images', "attachment_map_{$this->config['name']}", implode(',', $list));
106
    }
107
}
108