Passed
Push — master ( 9727f8...a3fb64 )
by Andreas
22:43
created

dbacontainer::lock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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