Completed
Push — master ( e0dfe2...066cbc )
by Andreas
36:15
created

dbacontainer::prepare_field()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 19
c 1
b 1
f 0
nc 7
nop 1
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 8
rs 8.4444
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\storage\container;
7
8
use midcom_core_dbaobject;
9
use midgard_reflection_property;
10
use midcom\datamanager\schema;
11
use midcom\datamanager\storage\transientnode;
12
use midcom\datamanager\storage\node;
13
use midcom\datamanager\storage\blobs;
14
use midcom\datamanager\storage\property;
15
use Symfony\Component\Validator\Constraints\Length;
16
17
/**
18
 * Experimental storage baseclass
19
 */
20
class dbacontainer extends container
21
{
22
    /**
23
     *
24
     * @var midcom_core_dbaobject
25
     */
26
    private $object;
27
28 218
    public function __construct(schema $schema, midcom_core_dbaobject $object, array $defaults)
29
    {
30 218
        $this->object = $object;
31 218
        $this->schema = $schema;
32
33 218
        foreach ($this->schema->get('fields') as $name => $config) {
34 218
            if (array_key_exists($name, $defaults)) {
35 52
                $config['default'] = $defaults[$name];
36
            }
37 218
            $config['name'] = $name;
38 218
            $field = $this->prepare_field($config);
39 218
            if (   isset($config['default'])
40 218
                && (!$this->object->id || $field instanceof transientnode)) {
41 50
                $field->set_value($config['default']);
42
            }
43
44 218
            $this->fields[$name] = $field;
45
        }
46 218
    }
47
48 90
    public function lock() : bool
49
    {
50 90
        if (!$this->object->id) {
51 46
            return true;
52
        }
53 44
        return $this->object->metadata->lock();
54
    }
55
56 12
    public function unlock() : bool
57
    {
58 12
        if (!$this->object->id) {
59
            return true;
60
        }
61 12
        if ($this->object->metadata->can_unlock()) {
62 12
            return $this->object->metadata->unlock();
63
        }
64
        return false;
65
    }
66
67 89
    public function is_locked() : bool
68
    {
69 89
        return $this->object->metadata->is_locked();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 130
    public function get_value()
76
    {
77 130
        return $this->object;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function set_value($object)
84
    {
85
        $this->object = $object;
86
    }
87
88 218
    private function prepare_field(array $config) : node
89
    {
90 218
        if (   empty($config['storage']['location'])
91
               // This line is needed because a parameter default is set by the schema parser and then ignored
92
               // by the type. The things we do for backwards compatibility...
93 218
            || $config['storage']['location'] === 'parameter') {
94 204
            if (class_exists('midcom\datamanager\storage\\' . $config['type'])) {
95 181
                $classname = 'midcom\datamanager\storage\\' . $config['type'];
96 136
            } elseif (strtolower($config['storage']['location']) === 'parameter') {
97 135
                $classname = 'midcom\datamanager\storage\parameter';
98
            } else {
99 204
                return new transientnode($config);
100
            }
101 207
        } elseif (strtolower($config['storage']['location']) === 'metadata') {
102 95
            $classname = 'midcom\datamanager\storage\metadata';
103 128
        } elseif (strtolower($config['storage']['location']) === 'privilege') {
104 5
            $classname = 'midcom\datamanager\storage\privilege';
105
        } else {
106 125
            $classname = property::class;
107
108 125
            $rfp = new midgard_reflection_property($this->object->__mgdschema_class_name__);
109 125
            $type = $rfp->get_midgard_type($config['storage']['location']);
110 125
            if ($type == MGD_TYPE_STRING) {
111 116
                $config['validation'][] = new Length(['max' => 255]);
112
            }
113
        }
114 218
        return new $classname($this->object, $config);
115
    }
116
117 15
    public function save()
118
    {
119 15
        if ($this->object->id) {
120 6
            $stat = $this->object->update();
121 9
        } elseif ($stat = $this->object->create()) {
122 9
            $this->object->set_parameter('midcom.helper.datamanager2', 'schema_name', $this->schema->get_name());
123
        }
124 15
        if (!$stat) {
125
            if (\midcom_connection::get_error() === MGD_ERR_ACCESS_DENIED) {
126
                throw new \midcom_error_forbidden('Failed to save: ' . \midcom_connection::get_error_string());
127
            }
128
            throw new \midcom_error('Failed to save: ' . \midcom_connection::get_error_string());
129
        }
130
131 15
        foreach ($this->fields as $node) {
132 15
            $node->save();
133
        }
134 15
    }
135
136
    public function move_uploaded_files() : int
137
    {
138
        $total_moved = 0;
139
        foreach ($this->fields as $node) {
140
            if ($node instanceof blobs) {
141
                $total_moved += $node->move_uploaded_files();
142
            }
143
        }
144
        return $total_moved;
145
    }
146
}
147