Passed
Push — master ( 5347c1...625fac )
by Andreas
18:24
created

image::save()   B

Complexity

Conditions 10
Paths 102

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 61.2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 24
nc 102
nop 0
dl 0
loc 37
ccs 5
cts 25
cp 0.2
crap 61.2
rs 7.65
c 1
b 0
f 0

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
 * 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
        if (array_intersect_key(array_flip(['description', 'title', 'score']), $this->value)) {
60
            $main = $this->get_main();
61
            $needs_update = false;
62
            if (array_key_exists('description', $this->value)) {
63
                $main->set_parameter('midcom.helper.datamanager2.type.blobs', 'description', $this->value['description']);
64
            }
65
            if (array_key_exists('title', $this->value)) {
66
                $needs_update = $main->title != $this->value['title'];
67
                $main->title = $this->value['title'];
68
            }
69
            if (array_key_exists('score', $this->value)) {
70
                $needs_update = $needs_update || $main->metadata->score != $this->value['score'];
71
                $main->metadata->score = (int) $this->value['score'];
72
            }
73
            if ($needs_update) {
74
                $main->update();
75
            }
76
        }
77 3
    }
78
79
    private function get_main() : \midcom_db_attachment
80
    {
81
        if (!empty($this->map['main'])) {
82
            return $this->map['main'];
83
        }
84
        return $this->load()['main'];
85
    }
86
}
87