Database   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 317
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 2
A exists() 0 4 1
A tables() 0 15 3
B install() 0 19 6
B uninstall() 0 35 5
B _table() 0 43 4
B _data() 0 42 4
A _dataCheck() 0 20 4
A _columns() 0 11 3
1
<?php
2
3
/**
4
 * Manage database content of a plugin
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Plugins
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\plugins;
17
18
/**
19
 * Manage database content of a plugin
20
 *
21
 * @category  Core
22
 * @package   Plugins
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
class Database
30
{
31
    /**
32
     * Plugin name
33
     **/
34
    private $_plugin = '';
35
36
    /**
37
     * Database driver object
38
     **/
39
    private $_database = null;
40
41
    /**
42
     * Database XML content of plugin
43
     **/
44
    private $_structure = [];
45
46
    /**
47
     * Store whether a database XML was found
48
     **/
49
    private $_exists = false;
50
51
    /**
52
     * Load plugin database content
53
     *
54
     * @param string $plugin Plugin name
55
     *
56
     * @return \csphere\core\plugins\Database
57
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
58
59
    public function __construct($plugin)
60
    {
61
        $this->_plugin = $plugin;
62
63
        $loader = \csphere\core\service\Locator::get();
64
65
        $this->_database = $loader->load('database');
66
67
        // Check for database XML file
68
        $path = \csphere\core\init\path();
69
        $file = $path . 'csphere/plugins/' . $this->_plugin . '/database.xml';
70
71
        if (file_exists($file)) {
72
73
            $this->_exists = true;
74
75
            // Get database content of plugin
76
            $xml = $loader->load('xml', 'database');
77
78
            $this->_structure = $xml->source('plugin', $plugin);
79
        }
80
    }
81
82
    /**
83
     * Looks for a database XML file inside the plugin
84
     *
85
     * @return boolean
86
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
87
88
    public function exists()
89
    {
90
        return $this->_exists;
91
    }
92
93
    /**
94
     * List of plugin tables as an array
95
     *
96
     * @return array
97
    **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
98
99
    public function tables()
100
    {
101
        // Check if plugin contains tables
102
        $tables = [];
103
104
        if (isset($this->_structure['tables'])) {
105
106
            foreach ($this->_structure['tables'] AS $table) {
107
108
                $tables[] = $table['name'];
109
            }
110
        }
111
112
        return $tables;
113
    }
114
115
    /**
116
     * Install plugin database content
117
     *
118
     * @param boolean $tables Whether to install tables
119
     * @param boolean $data   Whether to install data
120
     *
121
     * @return boolean
122
    **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
123
124
    public function install($tables, $data)
125
    {
126
        // Start with table creation
127
        if ($tables == true && isset($this->_structure['tables'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
128
129
            foreach ($this->_structure['tables'] AS $table) {
130
131
                $this->_table($table);
132
            }
133
        }
134
135
        // Go on with data afterwards
136
        if ($data == true && isset($this->_structure['data'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
137
138
            $this->_data($this->_structure['data']);
139
        }
140
141
        return true;
142
    }
143
144
    /**
145
     * Uninstall plugin database tables and options
146
     *
147
     * @param boolean $options Clear options table content
148
     *
149
     * @throws \Exception
150
     *
151
     * @return boolean
152
    **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
153
154
    public function uninstall($options = false)
155
    {
156
        // Remove all tables
157
        if (isset($this->_structure['tables'])) {
158
159
            foreach ($this->_structure['tables'] AS $table) {
160
161
                // Get table name and check prefix
162
                $name   = $table['name'];
163
                $prefix = substr($name, 0, strlen($this->_plugin));
164
165
                if ($prefix != $this->_plugin) {
166
167
                    throw new \Exception('Table name must begin with plugin name');
168
                }
169
170
                // Remove table itself
171
                $sql = \csphere\core\sql\DDL::drop($name);
172
173
                $this->_database->exec($sql['statement'], $sql['input'], true);
174
            }
175
        }
176
177
        // Remove all options
178
        if ($options == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
179
180
            $sql = \csphere\core\sql\DML::delete(
181
                'options', ['option_plugin', "=", $this->_plugin, false, false]
182
            );
183
184
            $this->_database->exec($sql['statement'], $sql['input']);
185
        }
186
187
        return true;
188
    }
189
190
    /**
191
     * Install plugin database table
192
     *
193
     * @param array $table Table structure as an array
194
     *
195
     * @throws \Exception
196
     *
197
     * @return void
198
    **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
199
200
    private function _table(array $table)
201
    {
202
        // Get table name and check prefix
203
        $name = $table['name'];
204
205
        $prefix = substr($name, 0, strlen($this->_plugin));
206
207
        if ($prefix != $this->_plugin) {
208
209
            throw new \Exception('Table name must begin with plugin name');
210
        }
211
212
        // Create table itself
213
        $columns  = $table['columns'];
214
        $primary  = $table['primary'];
215
        $foreigns = $table['foreigns'];
216
217
        $sql = \csphere\core\sql\DDL::create($name, $columns, $primary, $foreigns);
218
219
        $this->_database->exec($sql['statement'], $sql['input'], true);
220
221
        // Create unique indexes
222
        foreach ($table['uniques'] AS $unique) {
223
224
            $new = $unique['name'];
225
226
            $sql = \csphere\core\sql\DDL::index(
227
                $new, $name, $unique['column'], true
228
            );
229
230
            $this->_database->exec($sql['statement'], $sql['input']);
231
        }
232
233
        // Create all other indexes
234
        foreach ($table['indexes'] AS $index) {
235
236
            $new = $index['name'];
237
238
            $sql = \csphere\core\sql\DDL::index($new, $name, $index['column']);
239
240
            $this->_database->exec($sql['statement'], $sql['input']);
241
        }
242
    }
243
244
    /**
245
     * Install plugin database data
246
     *
247
     * @param array $data Data structure as an array
248
     *
249
     * @throws \Exception
250
     *
251
     * @return void
252
    **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
253
254
    private function _data(array $data)
255
    {
256
        $data = $this->_dataCheck($data);
257
258
        // Insert queries
259
        foreach ($data['insert'] AS $insert) {
260
261
            $columns = $this->_columns($insert['column']);
262
263
            $sql = \csphere\core\sql\DML::insert($insert['table'], $columns);
264
265
            $this->_database->exec($sql['statement'], $sql['input']);
266
        }
267
268
        // Update queries
269
        foreach ($data['update'] AS $update) {
270
271
            $columns = $this->_columns($update['column']);
272
273
            $where = $update['where'][0]['column'];
274
            $value = $update['where'][0]['value'];
275
276
            $sql = \csphere\core\sql\DML::update(
277
                $update['table'], $columns, $where, $value
278
            );
279
280
            $this->_database->exec($sql['statement'], $sql['input']);
281
        }
282
283
        // Delete queries
284
        foreach ($data['delete'] AS $delete) {
285
286
            $where = $delete['where'][0]['column'];
287
            $value = $delete['where'][0]['value'];
288
289
            $sql = \csphere\core\sql\DML::delete(
290
                $delete['table'], [$where, "=", $value, false, false]
291
            );
292
293
            $this->_database->exec($sql['statement'], $sql['input']);
294
        }
295
    }
296
297
    /**
298
     * Install plugin database data
299
     *
300
     * @param array $data Data structure as an array
301
     *
302
     * @return array
303
    **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
304
305
    private function _dataCheck(array $data)
306
    {
307
        // Make sure all parts are set
308
        if (!isset($data['insert'])) {
309
310
            $data['insert'] = [];
311
        }
312
313
        if (!isset($data['update'])) {
314
315
            $data['insert'] = [];
316
        }
317
318
        if (!isset($data['delete'])) {
319
320
            $data['insert'] = [];
321
        }
322
323
        return $data;
324
    }
325
326
    /**
327
     * Change array structure of columns for queries
328
     *
329
     * @param array $columns Columns as an array
330
     *
331
     * @return array
332
    **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
333
334
    private function _columns(array $columns)
335
    {
336
        $result = [];
337
338
        foreach ($columns AS $col) {
339
340
            $result[$col['name']] = isset($col['value']) ? $col['value'] : '';
341
        }
342
343
        return $result;
344
    }
345
}
346