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