Passed
Pull Request — master (#3070)
by Sergei
07:45
created

RemoveNamespacedAssets::acceptForeignKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 17
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0175
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema\Visitor;
6
7
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\Sequence;
10
use Doctrine\DBAL\Schema\Table;
11
12
/**
13
 * Removes assets from a schema that are not in the default namespace.
14
 *
15
 * Some databases such as MySQL support cross databases joins, but don't
16
 * allow to call DDLs to a database from another connected database.
17
 * Before a schema is serialized into SQL this visitor can cleanup schemas with
18
 * non default namespaces.
19
 *
20
 * This visitor filters all these non-default namespaced tables and sequences
21
 * and removes them from the SChema instance.
22
 */
23
final class RemoveNamespacedAssets extends AbstractVisitor
24
{
25
    /** @var Schema */
26
    private $schema;
27
28 81
    public function acceptSchema(Schema $schema) : void
29
    {
30 81
        $this->schema = $schema;
31 81
    }
32
33 81
    public function acceptTable(Table $table) : void
34
    {
35 81
        if ($table->isInDefaultNamespace($this->schema->getName())) {
36 81
            return;
37
        }
38
39 81
        $this->schema->dropTable($table->getName());
40 81
    }
41
42
    public function acceptSequence(Sequence $sequence) : void
43
    {
44
        if ($sequence->isInDefaultNamespace($this->schema->getName())) {
45
            return;
46
        }
47
48
        $this->schema->dropSequence($sequence->getName());
49
    }
50
51 54
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) : void
52
    {
53
        // The table may already be deleted in a previous
54
        // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
55
        // point to nowhere.
56 54
        if (! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
57 27
            $localTable->removeForeignKey($fkConstraint->getName());
58
59 27
            return;
60
        }
61
62 27
        $foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
63 27
        if ($foreignTable->isInDefaultNamespace($this->schema->getName())) {
64
            return;
65
        }
66
67 27
        $localTable->removeForeignKey($fkConstraint->getName());
68 27
    }
69
}
70