Completed
Pull Request — master (#591)
by Richard
16:26
created

PrefixStripper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 82
rs 10
c 0
b 0
f 0
ccs 16
cts 32
cp 0.5
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setTableFilter() 0 3 1
A addTable() 0 25 5
A __construct() 0 5 1
A addSequence() 0 9 2
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Database\Schema;
13
14
use Doctrine\DBAL\Schema\Table;
15
use Doctrine\DBAL\Schema\Schema;
16
use Doctrine\DBAL\Schema\Sequence;
17
18
/**
19
 * PrefixStripper extends Schema so we can easily add tables and
20
 * sequences selectively while visiting another schema.
21
 *
22
 * New schema will be stripped of database and prefix and optionally
23
 * filered by a table list
24
 *
25
 * @category  Xoops\Core\Database\Schema\PrefixStripper
26
 * @package   Xoops\Core
27
 * @author    Richard Griffith <[email protected]>
28
 * @copyright 2013 XOOPS Project (http://xoops.org)
29
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
30
 * @version   Release: 2.6
31
 * @link      http://xoops.org
32
 * @since     2.6.0
33
 */
34
class PrefixStripper extends Schema
35
{
36
37
    private $xPrefix = '';
38
    private $xDbName = '';
39
    private $tableList = array();
40
41
    /**
42
     * constructor
43
     */
44 13
    public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
0 ignored issues
show
Bug introduced by
The type Xoops\Core\Database\Schema\SchemaConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
    {
46 13
        $this->xPrefix = strtolower(\XoopsBaseConfig::get('db-prefix') . '_');
47 13
        $this->xDbName = strtolower(\XoopsBaseConfig::get('db-name'));
48 13
        parent::__construct($tables, $sequences, $schemaConfig);
49 13
    }
50
51
    /**
52
     * set list of tables to limit schema
53
     *
54
     * If no list is specified, all tables will be included
55
     *
56
     * @param array $tableList list of tables to include
57
     *
58
     * @return void
59
     */
60 2
    public function setTableFilter(array $tableList)
61
    {
62 2
        $this->tableList = $tableList;
63 2
    }
64
65
    /**
66
     * Add a table object to the schema
67
     *
68
     * @param Table $table table object to add
69
     *
70
     * @return void
71
     */
72 1
    public function addTable(Table $table)
73
    {
74
        //echo '<h2>addTable()</h2>';
75
        try {
76 1
            $name = $table->getName();
77 1
            $len = strlen($this->xPrefix);
78 1
            if (substr_compare($name, $this->xPrefix, 0, $len)===0) {
79
                $name = substr($name, $len);
80
                if (empty($this->tableList) || in_array($name, $this->tableList)) {
81
                    $idGeneratorType = 0; // how should we handle this?
82
                    $newtable = new Table(
83
                        $name,
84
                        $table->getColumns(),
85
                        $table->getIndexes(),
86
                        $table->getForeignKeys(),
87
                        $idGeneratorType,
88
                        $table->getOptions()
89
                    );
90 1
                    $this->_addTable($newtable);
91
                }
92
            }
93
            //Debug::dump($table);
94
        } catch (\Exception $e) {
95
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
96
            throw $e;
97
        }
98 1
    }
99
100
    /**
101
     * Add a sequence to the schema
102
     *
103
     * @param Sequence $sequence a sequence
104
     *
105
     * @return void
106
     */
107 1
    public function addSequence(Sequence $sequence)
108
    {
109
        //echo '<h2>addSequence()</h2>';
110
        try {
111 1
            $this->_addSequence($sequence);
112
            //Debug::dump($sequence);
113
        } catch (\Exception $e) {
114
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
115
            throw $e;
116
        }
117 1
    }
118
}
119