Failed Conditions
Push — 3.0.x ( 655b6b...430dce )
by Grégoire
16:57 queued 12:22
created

DropSchemaSqlCollector::acceptForeignKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0416
1
<?php
2
3
namespace Doctrine\DBAL\Schema\Visitor;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\SchemaException;
8
use Doctrine\DBAL\Schema\Sequence;
9
use Doctrine\DBAL\Schema\Table;
10
use SplObjectStorage;
11
use function assert;
12
use function strlen;
13
14
/**
15
 * Gathers SQL statements that allow to completely drop the current schema.
16
 */
17
class DropSchemaSqlCollector extends AbstractVisitor
18
{
19
    /** @var SplObjectStorage */
20
    private $constraints;
21
22
    /** @var SplObjectStorage */
23
    private $sequences;
24
25
    /** @var SplObjectStorage */
26
    private $tables;
27
28
    /** @var AbstractPlatform */
29
    private $platform;
30
31 110
    public function __construct(AbstractPlatform $platform)
32
    {
33 110
        $this->platform = $platform;
34 110
        $this->clearQueries();
35 110
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 66
    public function acceptTable(Table $table)
41
    {
42 66
        $this->tables->attach($table);
43 66
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 66
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
49
    {
50 66
        if (! $this->platform->supportsCreateDropForeignKeyConstraints()) {
51
            return;
52
        }
53
54 66
        if (strlen($fkConstraint->getName()) === 0) {
55 22
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
56
        }
57
58 44
        $this->constraints->attach($fkConstraint, $localTable);
59 44
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 22
    public function acceptSequence(Sequence $sequence)
65
    {
66 22
        $this->sequences->attach($sequence);
67 22
    }
68
69
    /**
70
     * @return void
71
     */
72 110
    public function clearQueries()
73
    {
74 110
        $this->constraints = new SplObjectStorage();
75 110
        $this->sequences   = new SplObjectStorage();
76 110
        $this->tables      = new SplObjectStorage();
77 110
    }
78
79
    /**
80
     * @return string[]
81
     */
82 88
    public function getQueries()
83
    {
84 88
        $sql = [];
85
86 88
        foreach ($this->constraints as $fkConstraint) {
87 44
            assert($fkConstraint instanceof ForeignKeyConstraint);
88 44
            $localTable = $this->constraints[$fkConstraint];
89 44
            $sql[]      = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
90
        }
91
92 88
        foreach ($this->sequences as $sequence) {
93 22
            assert($sequence instanceof Sequence);
94 22
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
95
        }
96
97 88
        foreach ($this->tables as $table) {
98 66
            assert($table instanceof Table);
99 66
            $sql[] = $this->platform->getDropTableSQL($table);
100
        }
101
102 88
        return $sql;
103
    }
104
}
105