Completed
Pull Request — 2.10 (#3762)
by Benjamin
21:30
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 135
    public function __construct(AbstractPlatform $platform)
31
    {
32 135
        $this->platform = $platform;
33 135
        $this->clearQueries();
34 135
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 131
    public function acceptTable(Table $table)
40
    {
41 131
        $this->tables->attach($table);
42 131
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 81
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
48
    {
49 81
        if (! $this->platform->supportsCreateDropForeignKeyConstraints()) {
50
            return;
51
        }
52
53 81
        if (strlen($fkConstraint->getName()) === 0) {
54 52
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
55
        }
56
57 79
        $this->constraints->attach($fkConstraint, $localTable);
58 79
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 27
    public function acceptSequence(Sequence $sequence)
64
    {
65 27
        $this->sequences->attach($sequence);
66 27
    }
67
68
    /**
69
     * @return void
70
     */
71 135
    public function clearQueries()
72
    {
73 135
        $this->constraints = new SplObjectStorage();
74 135
        $this->sequences   = new SplObjectStorage();
75 135
        $this->tables      = new SplObjectStorage();
76 135
    }
77
78
    /**
79
     * @return string[]
80
     */
81 133
    public function getQueries()
82
    {
83 133
        $sql = [];
84
85
        /** @var ForeignKeyConstraint $fkConstraint */
86 133
        foreach ($this->constraints as $fkConstraint) {
87 79
            $localTable = $this->constraints[$fkConstraint];
88 79
            $sql[]      = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
89
        }
90
91
        /** @var Sequence $sequence */
92 133
        foreach ($this->sequences as $sequence) {
93 27
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
94
        }
95
96
        /** @var Table $table */
97 133
        foreach ($this->tables as $table) {
98 131
            $sql[] = $this->platform->getDropTableSQL($table);
99
        }
100
101 133
        return $sql;
102
    }
103
}
104