Passed
Pull Request — master (#3233)
by Sergey
12:33
created

DropSchemaSqlCollector   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 28
dl 0
loc 95
rs 10
c 0
b 0
f 0
ccs 36
cts 36
cp 1

7 Methods

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