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