schemadb   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 39.66%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 121
ccs 23
cts 58
cp 0.3966
rs 10
c 2
b 0
f 0
wmc 26

8 Methods

Rating   Name   Duplication   Size   Complexity  
A from_path() 0 3 1
A add() 0 4 1
A all() 0 3 1
A get() 0 6 2
C check_inheritance() 0 68 16
A has() 0 3 1
A get_first() 0 6 2
A __construct() 0 5 2
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager;
7
8
use midcom_error;
9
use midcom_helper_misc;
10
11
/**
12
 * Experimental schemadb class
13
 */
14
class schemadb
15
{
16
    private array $schemas = [];
17
18 173
    public static function from_path(string $path) : self
19
    {
20 173
        return new static(midcom_helper_misc::load_snippet($path));
21
    }
22
23 193
    public function __construct(array $data = [])
24
    {
25 193
        $this->check_inheritance($data);
26 193
        foreach ($data as $name => $config) {
27 190
            $this->add($name, new schema($config));
28
        }
29
    }
30
31 193
    private function check_inheritance(array &$data)
32
    {
33 193
        foreach ($data as $schema_name => $schema) {
34 190
            if (!isset($schema['extends'])) {
35 190
                continue;
36
            }
37
38
            // Default extended schema is with the same name
39
            $extended_schema_name = $schema_name;
40
            $path = '';
41
42
            if (is_array($schema['extends'])) {
43
                if (isset($schema['extends']['path'])) {
44
                    $path = $schema['extends']['path'];
45
                }
46
47
                // Override schema name
48
                if (isset($schema['extends']['name'])) {
49
                    $extended_schema_name = $schema['extends']['name'];
50
                }
51
            } elseif (isset($data[$schema['extends']])) {
52
                $schema['extends'] = [
53
                    'name' => $schema['extends'],
54
                ];
55
            } else {
56
                $path = $schema['extends'];
57
            }
58
59
            if ($path === '') {
60
                // Infinite loop, set an UI message and stop executing
61
                if (   !isset($schema['extends']['name'])
62
                    || $schema['extends']['name'] === $schema_name) {
63
                    throw new midcom_error('schema ' . $schema_name . ' extends itself');
64
                }
65
                $extended_schema_name = $schema['extends']['name'];
66
                $extended_schemadb = [$extended_schema_name => $data[$extended_schema_name]];
67
            } else {
68
                $extended_schemadb = midcom_helper_misc::load_snippet($path);
69
                if (!isset($extended_schemadb[$extended_schema_name])) {
70
                    throw new midcom_error('extended schema ' . $path . ':' . $schema_name . ' was not found');
71
                }
72
            }
73
74
            // Override the extended schema with fields from the new schema
75
            foreach ($schema as $key => $value) {
76
                if ($key === 'extends') {
77
                    continue;
78
                }
79
80
                // This is probably either fields or operations
81
                if (is_array($value)) {
82
                    $extended_schemadb[$extended_schema_name][$key] ??= [];
83
84
                    foreach ($value as $name => $field) {
85
                        if (!$field) {
86
                            unset($extended_schemadb[$extended_schema_name][$key][$name]);
87
                            continue;
88
                        }
89
90
                        $extended_schemadb[$extended_schema_name][$key][$name] = $field;
91
                    }
92
                } else {
93
                    $extended_schemadb[$extended_schema_name][$key] = $value;
94
                }
95
            }
96
97
            // Replace the new schema with extended schema
98
            $data[$schema_name] = $extended_schemadb[$extended_schema_name];
99
        }
100
    }
101
102 193
    public function add(string $name, schema $schema)
103
    {
104 193
        $this->schemas[$name] = $schema;
105 193
        $schema->set_name($name);
106
    }
107
108
    /**
109
     * @return schema[]
110
     */
111 34
    public function all() : array
112
    {
113 34
        return $this->schemas;
114
    }
115
116 119
    public function get_first() : schema
117
    {
118 119
        if (empty($this->schemas)) {
119
            throw new midcom_error('Schema DB is empty');
120
        }
121 119
        return reset($this->schemas);
122
    }
123
124 65
    public function has(string $name) : bool
125
    {
126 65
        return array_key_exists($name, $this->schemas);
127
    }
128
129 81
    public function get(string $name) : schema
130
    {
131 81
        if (!array_key_exists($name, $this->schemas)) {
132
            throw new midcom_error('Schema ' . $name . ' not found in schemadb');
133
        }
134 81
        return $this->schemas[$name];
135
    }
136
}
137