Failed Conditions
Pull Request — master (#3233)
by Sergey
10:50
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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Schema\Visitor;
21
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
use Doctrine\DBAL\Schema\Table;
24
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
25
use Doctrine\DBAL\Schema\Sequence;
26
use Doctrine\DBAL\Schema\View;
27
use Doctrine\DBAL\Schema\SchemaException;
28
use function strlen;
29
30
/**
31
 * Gathers SQL statements that allow to completely drop the current schema.
32
 *
33
 * @link   www.doctrine-project.org
34
 * @since  2.0
35
 * @author Benjamin Eberlei <[email protected]>
36
 */
37
class DropSchemaSqlCollector extends AbstractVisitor
38
{
39
    /**
40
     * @var \SplObjectStorage
41
     */
42
    private $constraints;
43
44
    /**
45
     * @var \SplObjectStorage
46
     */
47
    private $sequences;
48
49
    /**
50
     * @var \SplObjectStorage
51
     */
52
    private $tables;
53
54
    /**
55
     * @var \SplObjectStorage
56
     */
57
    private $views;
58
59
    /**
60
     * @var AbstractPlatform
61
     */
62
    private $platform;
63
64
    /**
65
     * @param AbstractPlatform $platform
66
     */
67 120
    public function __construct(AbstractPlatform $platform)
68
    {
69 120
        $this->platform = $platform;
70 120
        $this->clearQueries();
71 120
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 60
    public function acceptTable(Table $table)
77
    {
78 60
        $this->tables->attach($table);
79 60
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 60
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
85
    {
86 60
        if (strlen($fkConstraint->getName()) == 0) {
87 20
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
88
        }
89
90 40
        $this->constraints->attach($fkConstraint, $localTable);
91 40
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 20
    public function acceptSequence(Sequence $sequence)
97
    {
98 20
        $this->sequences->attach($sequence);
99 20
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 20
    public function acceptView(View $view)
105
    {
106 20
        $this->views->attach($view);
107 20
    }
108
109
    /**
110
     * @return void
111
     */
112 120
    public function clearQueries()
113
    {
114 120
        $this->constraints = new \SplObjectStorage();
115 120
        $this->sequences = new \SplObjectStorage();
116 120
        $this->tables = new \SplObjectStorage();
117 120
        $this->views = new \SplObjectStorage();
118 120
    }
119
120
    /**
121
     * @return array
122
     */
123 100
    public function getQueries()
124
    {
125 100
        $sql = [];
126
127 100
        foreach ($this->constraints as $fkConstraint) {
128 40
            $localTable = $this->constraints[$fkConstraint];
129 40
            $sql[] = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
130
        }
131
132 100
        foreach ($this->sequences as $sequence) {
133 20
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
134
        }
135
136 100
        foreach ($this->tables as $table) {
137 60
            $sql[] = $this->platform->getDropTableSQL($table);
138
        }
139
140 100
        foreach ($this->views as $view) {
141 20
            $sql[] = $this->platform->getDropViewSQL($view->getName());
142
        }
143
144 100
        return $sql;
145
    }
146
}
147