Passed
Pull Request — master (#3233)
by Sergey
12:25
created

RemoveNamespacedAssets   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 57.69%

Importance

Changes 0
Metric Value
wmc 10
eloc 19
dl 0
loc 68
rs 10
c 0
b 0
f 0
ccs 15
cts 26
cp 0.5769

5 Methods

Rating   Name   Duplication   Size   Complexity  
A acceptSchema() 0 3 1
A acceptTable() 0 7 2
A acceptSequence() 0 7 2
A acceptView() 0 7 2
A acceptForeignKey() 0 16 3
1
<?php
2
3
namespace Doctrine\DBAL\Schema\Visitor;
4
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\DBAL\Schema\Sequence;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\DBAL\Schema\View;
10
11
/**
12
 * Removes assets from a schema that are not in the default namespace.
13
 *
14
 * Some databases such as MySQL support cross databases joins, but don't
15
 * allow to call DDLs to a database from another connected database.
16
 * Before a schema is serialized into SQL this visitor can cleanup schemas with
17
 * non default namespaces.
18
 *
19
 * This visitor filters all these non-default namespaced tables and sequences
20
 * and removes them from the SChema instance.
21
 */
22
class RemoveNamespacedAssets extends AbstractVisitor
23
{
24
    /** @var Schema */
25
    private $schema;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 57
    public function acceptSchema(Schema $schema)
31
    {
32 57
        $this->schema = $schema;
33 57
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 57
    public function acceptTable(Table $table)
39
    {
40 57
        if ($table->isInDefaultNamespace($this->schema->getName())) {
41 57
            return;
42
        }
43
44 57
        $this->schema->dropTable($table->getName());
45 57
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function acceptSequence(Sequence $sequence)
51
    {
52
        if ($sequence->isInDefaultNamespace($this->schema->getName())) {
53
            return;
54
        }
55
56
        $this->schema->dropSequence($sequence->getName());
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function acceptView(View $view)
63
    {
64
        if ($view->isInDefaultNamespace($this->schema->getName())) {
65
            return;
66
        }
67
68
        $this->schema->dropView($view->getName());
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 38
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
75
    {
76
        // The table may already be deleted in a previous
77
        // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
78
        // point to nowhere.
79 38
        if (! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
80 19
            $localTable->removeForeignKey($fkConstraint->getName());
81 19
            return;
82
        }
83
84 19
        $foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
85 19
        if ($foreignTable->isInDefaultNamespace($this->schema->getName())) {
86
            return;
87
        }
88
89 19
        $localTable->removeForeignKey($fkConstraint->getName());
90 19
    }
91
}
92