Completed
Pull Request — 2.10.x (#3984)
by Craig
61:21 queued 57:56
created

DropSchemaSqlCollector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 86
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A acceptTable() 0 3 1
A acceptSequence() 0 3 1
A clearQueries() 0 5 1
A getQueries() 0 21 4
A acceptForeignKey() 0 11 3
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 strlen;
12
13
/**
14
 * Gathers SQL statements that allow to completely drop the current schema.
15
 */
16
class DropSchemaSqlCollector extends AbstractVisitor
17
{
18
    /** @var SplObjectStorage */
19
    private $constraints;
20
21
    /** @var SplObjectStorage */
22
    private $sequences;
23
24
    /** @var SplObjectStorage */
25
    private $tables;
26
27
    /** @var AbstractPlatform */
28
    private $platform;
29
30 120
    public function __construct(AbstractPlatform $platform)
31
    {
32 120
        $this->platform = $platform;
33 120
        $this->clearQueries();
34 120
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 118
    public function acceptTable(Table $table)
40
    {
41 118
        $this->tables->attach($table);
42 118
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 72
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
48
    {
49 72
        if (! $this->platform->supportsCreateDropForeignKeyConstraints()) {
50
            return;
51
        }
52
53 72
        if (strlen($fkConstraint->getName()) === 0) {
54 47
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
55
        }
56
57 71
        $this->constraints->attach($fkConstraint, $localTable);
58 71
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 24
    public function acceptSequence(Sequence $sequence)
64
    {
65 24
        $this->sequences->attach($sequence);
66 24
    }
67
68
    /**
69
     * @return void
70
     */
71 120
    public function clearQueries()
72
    {
73 120
        $this->constraints = new SplObjectStorage();
74 120
        $this->sequences   = new SplObjectStorage();
75 120
        $this->tables      = new SplObjectStorage();
76 120
    }
77
78
    /**
79
     * @return string[]
80
     */
81 119
    public function getQueries()
82
    {
83 119
        $sql = [];
84
85
        /** @var ForeignKeyConstraint $fkConstraint */
86 119
        foreach ($this->constraints as $fkConstraint) {
87 71
            $localTable = $this->constraints[$fkConstraint];
88 71
            $sql[]      = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
89
        }
90
91
        /** @var Sequence $sequence */
92 119
        foreach ($this->sequences as $sequence) {
93 24
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
94
        }
95
96
        /** @var Table $table */
97 119
        foreach ($this->tables as $table) {
98 118
            $sql[] = $this->platform->getDropTableSQL($table);
99
        }
100
101 119
        return $sql;
102
    }
103
}
104