Completed
Push — master ( 3b6e69...cc7bc1 )
by Grégoire
20s queued 20s
created

DropSchemaSqlCollector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 74
rs 10
c 0
b 0
f 0
ccs 34
cts 35
cp 0.9714
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A acceptTable() 0 3 1
A __construct() 0 4 1
A acceptSequence() 0 3 1
A clearQueries() 0 5 1
A getQueries() 0 21 4
A acceptForeignKey() 0 11 3
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