Completed
Push — master ( 160801...bd59d4 )
by Andreas
27:08
created

image   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 15.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
dl 0
loc 81
ccs 7
cts 46
cp 0.1522
rs 10
c 1
b 0
f 0
wmc 19

3 Methods

Rating   Name   Duplication   Size   Complexity  
A recreate() 0 14 3
A get_main() 0 10 3
C save() 0 41 13
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
 * Image storage
12
 *
13
 * Controls a list of images derived from just one (via filter chains)
14
 */
15
class image extends blobs implements recreateable
16
{
17
    protected $save_archival = false;
18
19
    public function recreate() : bool
20
    {
21
        $existing = $this->load();
22
        if (array_key_exists('archival', $existing)) {
23
            $attachment = $existing['archival'];
24
        } elseif (array_key_exists('main', $existing)) {
25
            $attachment = $existing['main'];
26
        } else {
27
            return true;
28
        }
29
30
        $filter = new imagefilter($this->config['type_config'], $this->save_archival);
31
        $this->map = $filter->process($attachment, $existing);
32
        return $this->save_attachment_list();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function save()
39
    {
40 3
        if ($this->value === null) {
41
            //delete?
42
        }
43
44 3
        if (!empty($this->value['delete'])) {
45
            $this->map = [];
46
            $this->save_attachment_list();
47
            return;
48
        }
49
50 3
        if (!empty($this->value['file'])) {
51
            $this->value['file']->parentguid = $this->object->guid;
52
            $existing = $this->load();
53
            $filter = new imagefilter($this->config['type_config'], $this->save_archival);
54
            $this->map = $filter->process($this->value['file'], $existing);
55
56
            $this->save_attachment_list();
57
        }
58
59 3
        $check_fields = ['description', 'title', 'score'];
60
61 3
        if (array_intersect_key(array_keys($check_fields), $this->value)) {
62
            $main = $this->get_main();
63
            $needs_update = false;
64
            foreach ($check_fields as $field) {
65
                if (array_key_exists($field, $this->value)) {
66
                    if ($field === 'description') {
67
                        $main->set_parameter('midcom.helper.datamanager2.type.blobs', 'description', $this->value['description']);
68
                    } elseif ($field === 'title') {
69
                        $needs_update = $needs_update || $main->title != $this->value['title'];
70
                        $main->title = $this->value['title'];
71
                    } elseif ($field === 'score') {
72
                        $needs_update = $needs_update || $main->metadata->score != $this->value['score'];
73
                        $main->metadata->score = (int) $this->value['score'];
74
                    }
75
                }
76
            }
77
            if ($needs_update) {
78
                $main->update();
79
            }
80
        }
81 3
    }
82
83
    /**
84
     * @return \midcom_db_attachment
85
     */
86
    private function get_main()
87
    {
88
        if (!empty($this->map['main'])) {
89
            return $this->map['main'];
90
        }
91
        $items = $this->load();
92
        if (!empty($items['main'])) {
93
            return $items['main'];
94
        }
95
        return false;
96
    }
97
}
98