Completed
Pull Request — master (#3233)
by Sergey
65:15
created

DropSchemaSqlCollector::acceptView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
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 Doctrine\DBAL\Schema\View;
11
use SplObjectStorage;
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 SplObjectStorage<View> */
29
    private $views;
30 95
31
    /** @var AbstractPlatform */
32 95
    private $platform;
33 95
34 95
    public function __construct(AbstractPlatform $platform)
35
    {
36
        $this->platform = $platform;
37
        $this->clearQueries();
38
    }
39 57
40
    /**
41 57
     * {@inheritdoc}
42 57
     */
43
    public function acceptTable(Table $table)
44
    {
45
        $this->tables->attach($table);
46
    }
47 57
48
    /**
49 57
     * {@inheritdoc}
50 19
     */
51
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
52
    {
53 38
        if (strlen($fkConstraint->getName()) === 0) {
54 38
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
55
        }
56
57
        $this->constraints->attach($fkConstraint, $localTable);
58
    }
59 19
60
    /**
61 19
     * {@inheritdoc}
62 19
     */
63
    public function acceptSequence(Sequence $sequence)
64
    {
65
        $this->sequences->attach($sequence);
66
    }
67 95
68
    /**
69 95
     * {@inheritdoc}
70 95
     */
71 95
    public function acceptView(View $view)
72 95
    {
73
        $this->views->attach($view);
74
    }
75
76
    /**
77 76
     * @return void
78
     */
79 76
    public function clearQueries()
80
    {
81 76
        $this->constraints = new SplObjectStorage();
82 38
        $this->sequences   = new SplObjectStorage();
83 38
        $this->tables      = new SplObjectStorage();
84
        $this->views       = new SplObjectStorage();
85
    }
86 76
87 19
    /**
88
     * @return string[]
89
     */
90 76
    public function getQueries()
91 57
    {
92
        $sql = [];
93
94 76
        foreach ($this->constraints as $fkConstraint) {
95
            $localTable = $this->constraints[$fkConstraint];
96
            $sql[]      = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
97
        }
98
99
        foreach ($this->sequences as $sequence) {
100
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
101
        }
102
103
        foreach ($this->tables as $table) {
104
            $sql[] = $this->platform->getDropTableSQL($table);
105
        }
106
107
        foreach ($this->views as $view) {
108
            $sql[] = $this->platform->getDropViewSQL($view->getName());
109
        }
110
111
        return $sql;
112
    }
113
}
114