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