Failed Conditions
Pull Request — develop (#3348)
by Sergei
125:02 queued 59:58
created

RemoveNamespacedAssets::removeForeignKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\SchemaException;
8
use Doctrine\DBAL\Schema\Sequence;
9
use Doctrine\DBAL\Schema\Table;
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 75
     */
30
    public function acceptSchema(Schema $schema) : void
31 75
    {
32 75
        $this->schema = $schema;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37 75
     */
38
    public function acceptTable(Table $table) : void
39 75
    {
40 75
        if ($table->isInDefaultNamespace($this->schema->getName())) {
41
            return;
42
        }
43 75
44 75
        $this->schema->dropTable($table->getName());
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function acceptSequence(Sequence $sequence) : void
51
    {
52
        if ($sequence->isInDefaultNamespace($this->schema->getName())) {
53
            return;
54
        }
55
56
        $this->schema->dropSequence($sequence->getName());
57
    }
58
59
    /**
60
     * {@inheritdoc}
61 50
     *
62
     * @throws SchemaException
63
     */
64
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) : void
65
    {
66 50
        // The table may already be deleted in a previous
67 49
        // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
68 49
        // point to nowhere.
69
        if (! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
70
            $this->removeForeignKey($localTable, $fkConstraint);
71 25
72 25
            return;
73
        }
74
75
        $foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
76 25
        if ($foreignTable->isInDefaultNamespace($this->schema->getName())) {
77 25
            return;
78
        }
79
80
        $this->removeForeignKey($localTable, $fkConstraint);
81
    }
82
83
    /**
84
     * @throws SchemaException
85
     */
86
    private function removeForeignKey(Table $table, ForeignKeyConstraint $constraint) : void
87
    {
88
        $name = $constraint->getName();
89
90
        if ($name === null) {
91
            throw SchemaException::namedForeignKeyRequired($table, $constraint);
92
        }
93
94
        $table->removeForeignKey($name);
95
    }
96
}
97