Completed
Push — master ( 593857...cd1c2d )
by Michael
29s queued 20s
created

PrefixStripper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 79.31%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 71
ccs 23
cts 29
cp 0.7931
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addTable() 0 23 5
A __construct() 0 5 1
A addSequence() 0 7 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\SchemaConfig;
17
use Doctrine\DBAL\Schema\Sequence;
18
19
/**
20
 * PrefixStripper extends Schema so we can easily add tables and
21
 * sequences selectively while visiting another schema.
22
 *
23
 * New schema will be stripped of database and prefix and optionally
24
 * filtered by a table list
25
 *
26
 * @category  Xoops\Core\Database\Schema\PrefixStripper
27
 * @package   Xoops\Core
28
 * @author    Richard Griffith <[email protected]>
29
 * @copyright 2013-2019 XOOPS Project (https://xoops.org)
30
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
31
 */
32
class PrefixStripper extends Schema
33
{
34
35
    private $xPrefix;
36
    private $tableList = [];
37
38
    /**
39
     * constructor
40
     *
41
     * @param string       $prefix       Required table name prefix to remove
42
     * @param string[]     $tableList    list of table names (without prefix) to include
43
     * @param SchemaConfig $schemaConfig SchemaConfig object to include in schema
44
     */
45 13
    public function __construct(string $prefix, array $tableList = [], SchemaConfig $schemaConfig = null)
46
    {
47 13
        $this->xPrefix = $prefix;
48 13
        $this->tableList = $tableList;
49 13
        parent::__construct([], [], $schemaConfig);
50 13
    }
51
52
    /**
53
     * Add a table object to the schema
54
     *
55
     * @param Table $table table object to add
56
     *
57
     * @return void
58
     *
59
     * @throws \Doctrine\DBAL\DBALException
60
     */
61 4
    public function addTable(Table $table)
62
    {
63
        try {
64 4
            $name = $table->getName();
65 4
            $len = strlen($this->xPrefix);
66 4
            if (substr_compare($name, $this->xPrefix, 0, $len)===0) {
67 3
                $name = substr($name, $len);
68 3
                if (empty($this->tableList) || in_array($name, $this->tableList)) {
69 3
                    $idGeneratorType = 0; // how should we handle this?
70 3
                    $newtable = new Table(
71 3
                        $name,
72 3
                        $table->getColumns(),
73 3
                        $table->getIndexes(),
74 3
                        $table->getForeignKeys(),
75 3
                        $idGeneratorType,
76 3
                        $table->getOptions()
77
                    );
78 4
                    $this->_addTable($newtable);
79
                }
80
            }
81
        } catch (\Doctrine\DBAL\DBALException $e) {
82
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
83
            throw $e;
84
        }
85 4
    }
86
87
    /**
88
     * Add a sequence to the schema
89
     *
90
     * @param Sequence $sequence a sequence
91
     *
92
     * @return void
93
     *
94
     * @throws \Doctrine\DBAL\Schema\SchemaException
95
     */
96 2
    public function addSequence(Sequence $sequence)
97
    {
98
        try {
99 2
            $this->_addSequence($sequence);
100
        } catch (\Doctrine\DBAL\Schema\SchemaException $e) {
101
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
102
            throw $e;
103
        }
104 2
    }
105
}
106