Completed
Push — master ( 3490f3...91fee2 )
by Andreas
30:00
created

dbacontainer::set_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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