Completed
Push — 2.11.x ( 0a2e2f...a8544c )
by Grégoire
23s queued 16s
created

DropSchemaSqlCollector::getQueries()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.8666
c 0
b 0
f 0
cc 4
nc 8
nop 0
crap 4
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 120
    public function __construct(AbstractPlatform $platform)
32
    {
33 120
        $this->platform = $platform;
34 120
        $this->clearQueries();
35 120
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 118
    public function acceptTable(Table $table)
41
    {
42 118
        $this->tables->attach($table);
43 118
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 72
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
49
    {
50 72
        if (! $this->platform->supportsCreateDropForeignKeyConstraints()) {
51
            return;
52
        }
53
54 72
        if (strlen($fkConstraint->getName()) === 0) {
55 47
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
56
        }
57
58 71
        $this->constraints->attach($fkConstraint, $localTable);
59 71
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 24
    public function acceptSequence(Sequence $sequence)
65
    {
66 24
        $this->sequences->attach($sequence);
67 24
    }
68
69
    /**
70
     * @return void
71
     */
72 120
    public function clearQueries()
73
    {
74 120
        $this->constraints = new SplObjectStorage();
75 120
        $this->sequences   = new SplObjectStorage();
76 120
        $this->tables      = new SplObjectStorage();
77 120
    }
78
79
    /**
80
     * @return string[]
81
     */
82 119
    public function getQueries()
83
    {
84 119
        $sql = [];
85
86 119
        foreach ($this->constraints as $fkConstraint) {
87 71
            assert($fkConstraint instanceof ForeignKeyConstraint);
88 71
            $localTable = $this->constraints[$fkConstraint];
89 71
            $sql[]      = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
90
        }
91
92 119
        foreach ($this->sequences as $sequence) {
93 24
            assert($sequence instanceof Sequence);
94 24
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
95
        }
96
97 119
        foreach ($this->tables as $table) {
98 118
            assert($table instanceof Table);
99 118
            $sql[] = $this->platform->getDropTableSQL($table);
100
        }
101
102 119
        return $sql;
103
    }
104
}
105