Completed
Pull Request — master (#4001)
by Grégoire
17:58 queued 03:26
created

DropSchemaSqlCollector::acceptTable()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema\Visitor;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Schema\Exception\NamedForeignKeyRequired;
9
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
10
use Doctrine\DBAL\Schema\Sequence;
11
use Doctrine\DBAL\Schema\Table;
12
use SplObjectStorage;
13
use function assert;
14
use function strlen;
15
16
/**
17
 * Gathers SQL statements that allow to completely drop the current schema.
18
 */
19
class DropSchemaSqlCollector extends AbstractVisitor
20
{
21
    /** @var SplObjectStorage */
22
    private $constraints;
23
24
    /** @var SplObjectStorage */
25
    private $sequences;
26
27
    /** @var SplObjectStorage */
28
    private $tables;
29
30
    /** @var AbstractPlatform */
31
    private $platform;
32
33 110
    public function __construct(AbstractPlatform $platform)
34
    {
35 110
        $this->platform = $platform;
36 110
        $this->clearQueries();
37 110
    }
38
39 66
    public function acceptTable(Table $table) : void
40
    {
41 66
        $this->tables->attach($table);
42 66
    }
43
44 66
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) : void
45
    {
46 66
        if (! $this->platform->supportsCreateDropForeignKeyConstraints()) {
47
            return;
48
        }
49
50 66
        if (strlen($fkConstraint->getName()) === 0) {
51 22
            throw NamedForeignKeyRequired::new($localTable, $fkConstraint);
52
        }
53
54 44
        $this->constraints->attach($fkConstraint, $localTable);
55 44
    }
56
57 22
    public function acceptSequence(Sequence $sequence) : void
58
    {
59 22
        $this->sequences->attach($sequence);
60 22
    }
61
62 110
    public function clearQueries() : void
63
    {
64 110
        $this->constraints = new SplObjectStorage();
65 110
        $this->sequences   = new SplObjectStorage();
66 110
        $this->tables      = new SplObjectStorage();
67 110
    }
68
69
    /**
70
     * @return array<int, string>
71
     */
72 88
    public function getQueries() : array
73
    {
74 88
        $sql = [];
75
76 88
        foreach ($this->constraints as $fkConstraint) {
77 44
            assert($fkConstraint instanceof ForeignKeyConstraint);
78 44
            $localTable = $this->constraints[$fkConstraint];
79 44
            $sql[]      = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
80
        }
81
82 88
        foreach ($this->sequences as $sequence) {
83 22
            assert($sequence instanceof Sequence);
84 22
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
85
        }
86
87 88
        foreach ($this->tables as $table) {
88 66
            assert($table instanceof Table);
89 66
            $sql[] = $this->platform->getDropTableSQL($table);
90
        }
91
92 88
        return $sql;
93
    }
94
}
95