Completed
Pull Request — develop (#3348)
by Sergei
63:43
created

Sequence::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Schema\Visitor\Visitor;
8
use function assert;
9
use function count;
10
use function is_string;
11
use function sprintf;
12
13
/**
14
 * Sequence structure.
15
 */
16
class Sequence extends AbstractAsset
17
{
18
    /** @var int */
19
    protected $allocationSize = 1;
20
21
    /** @var int */
22
    protected $initialValue = 1;
23
24
    /** @var int|null */
25
    protected $cache = null;
26
27
    public function __construct(string $name, int $allocationSize = 1, int $initialValue = 1, ?int $cache = null)
28
    {
29
        $this->_setName($name);
30
        $this->setAllocationSize($allocationSize);
31 1050
        $this->setInitialValue($initialValue);
32
        $this->cache = $cache;
33 1050
    }
34 1050
35 1050
    public function getName() : string
36 1050
    {
37 1050
        $name = parent::getName();
38
        assert(is_string($name));
39
40
        return $name;
41
    }
42 1012
43
    public function getAllocationSize() : int
44 1012
    {
45
        return $this->allocationSize;
46
    }
47
48
    public function getInitialValue() : int
49
    {
50 1010
        return $this->initialValue;
51
    }
52 1010
53
    public function getCache() : ?int
54
    {
55
        return $this->cache;
56
    }
57
58 1000
    public function setAllocationSize(int $allocationSize) : self
59
    {
60 1000
        $this->allocationSize = $allocationSize;
61
62
        return $this;
63
    }
64
65
    public function setInitialValue(int $initialValue) : self
66
    {
67
        $this->initialValue = $initialValue;
68 1050
69
        return $this;
70 1050
    }
71
72 1050
    public function setCache(int $cache) : self
73
    {
74
        $this->cache = $cache;
75
76
        return $this;
77
    }
78
79
    /**
80 1050
     * Checks if this sequence is an autoincrement sequence for a given table.
81
     *
82 1050
     * This is used inside the comparator to not report sequences as missing,
83
     * when the "from" schema implicitly creates the sequences.
84 1050
     */
85
    public function isAutoIncrementsFor(Table $table) : bool
86
    {
87
        $primaryKey = $table->getPrimaryKey();
88
89
        if ($primaryKey === null) {
90
            return false;
91
        }
92
93
        $pkColumns = $primaryKey->getColumns();
94
95
        if (count($pkColumns) !== 1) {
96
            return false;
97
        }
98
99
        $column = $table->getColumn($pkColumns[0]);
100
101
        if (! $column->getAutoincrement()) {
102
            return false;
103
        }
104
105
        $sequenceName      = $this->getShortestName($table->getNamespaceName());
106
        $tableName         = $table->getShortestName($table->getNamespaceName());
107 433
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
108
109 433
        return $tableSequenceName === $sequenceName;
110
    }
111 433
112
    public function visit(Visitor $visitor) : void
113
    {
114
        $visitor->acceptSequence($this);
115 433
    }
116
}
117