Schema   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 29 4
A _settings() 0 6 1
A _cache() 0 23 3
B _open() 0 35 5
1
<?php
2
3
/**
4
 * Format and cache database xml details for usage
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Datamapper
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\datamapper;
17
18
/**
19
 * Format and cache database xml details for usage
20
 *
21
 * @category  Core
22
 * @package   Datamapper
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Schema
30
{
31
    /**
32
     * Service loader object
33
     **/
34
    private static $_loader = null;
35
36
    /**
37
     * XML driver object
38
     **/
39
    private static $_xml = null;
40
41
    /**
42
     * Cache driver object
43
     **/
44
    private static $_cache = null;
45
46
     /**
47
     * Store database files that are already opened
48
     **/
49
    private static $_loaded = [];
50
51
    /**
52
     * Load schema for database table
53
     *
54
     * @param string $plugin Plugin name
55
     * @param string $table  Table name without plugin_ prefix
56
     *
57
     * @throws \Exception
58
     *
59
     * @return array
60
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
61
62
    public static function load($plugin, $table = '')
63
    {
64
        // Empty table means that it is named like the plugin
65
        $schema = $plugin;
66
67
        if ($table != '') {
68
69
            $schema .= '_' . $table;
70
        }
71
72
        // Check if database structure is already loaded
73
        if (!isset(self::$_loaded[$plugin])) {
74
75
            self::$_loaded[$plugin] = self::_cache($plugin);
76
        }
77
78
        // Return info array if it exists
79
        if (isset(self::$_loaded[$plugin][$schema])) {
80
81
            return self::$_loaded[$plugin][$schema];
82
83
        } else {
84
85
            // Full table name (schema) should lead to less confusion
86
            $msg = 'Plugin "' . $plugin . '" lacks a database table:' . $schema;
87
88
            throw new \Exception($msg);
89
        }
90
    }
91
92
    /**
93
     * Sets options to work with database files
94
     *
95
     * @return void
96
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
97
98
    private static function _settings()
99
    {
100
        self::$_loader = \csphere\core\service\Locator::get();
101
102
        self::$_cache = self::$_loader->load('cache');
103
    }
104
105
    /**
106
     * Delivers the requested database file
107
     *
108
     * @param string $plugin Plugin of database file
109
     *
110
     * @return array
111
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
112
113
    private static function _cache($plugin)
114
    {
115
        // Load cache and settings if not done yet
116
        if (self::$_cache == null) {
117
118
            self::_settings();
119
        }
120
121
        $token = 'dm_schema_' . $plugin;
122
123
        // Look for plugin inside cache
124
        $schema = self::$_cache->load($token);
125
126
        // If cache loading fails load it and create cache file
127
        if ($schema == false) {
128
129
            $schema = self::_open($plugin);
130
131
            self::$_cache->save($token, $schema);
132
        }
133
134
        return $schema;
135
    }
136
137
    /**
138
     * Gets the content for the requested database file
139
     *
140
     * @param string $plugin Plugin of database file
141
     *
142
     * @return array
143
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
144
145
    private static function _open($plugin)
146
    {
147
        // Set XML loader if not done yet
148
        if (self::$_xml == null) {
149
150
            self::$_xml = self::$_loader->load('xml', 'database');
151
        }
152
153
        // Get data array of XML file
154
        $data = self::$_xml->source('plugin', $plugin);
155
156
        // Convert table data for later usage
157
        $schema = [];
158
159
        foreach ($data['tables'] AS $table) {
160
161
            // Mark serial column name of table
162
            $serial = '';
163
164
            foreach ($table['columns'] AS $column) {
165
166
                if ($column['datatype'] == 'serial') {
167
168
                    $serial = $column['name'];
169
                }
170
            }
171
172
            // Build schema array for caching
173
            $add = ['serial' => $serial];
174
175
            $schema[$table['name']] = array_merge($table, $add);
176
        }
177
178
        return $schema;
179
    }
180
}
181