Failed Conditions
Pull Request — develop (#3348)
by Sergei
65:27
created

Sequence::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
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
        $this->setInitialValue($initialValue);
30 903
        $this->cache = $cache;
31
    }
32 903
33 903
    public function getName() : string
34 903
    {
35 903
        $name = parent::getName();
36 903
        assert(is_string($name));
37
38
        return $name;
39
    }
40
41 480
    public function getAllocationSize() : int
42
    {
43 480
        return $this->allocationSize;
44
    }
45
46
    public function getInitialValue() : int
47
    {
48
        return $this->initialValue;
49 457
    }
50
51 457
    public function getCache() : ?int
52
    {
53
        return $this->cache;
54
    }
55
56
    public function setAllocationSize(int $allocationSize) : self
57 332
    {
58
        $this->allocationSize = $allocationSize;
59 332
60
        return $this;
61
    }
62
63
    public function setInitialValue(int $initialValue) : self
64
    {
65
        $this->initialValue = $initialValue;
66
67 903
        return $this;
68
    }
69 903
70
    public function setCache(int $cache) : self
71 903
    {
72
        $this->cache = $cache;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Checks if this sequence is an autoincrement sequence for a given table.
79 903
     *
80
     * This is used inside the comparator to not report sequences as missing,
81 903
     * when the "from" schema implicitly creates the sequences.
82
     */
83 903
    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
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
106 92
107
        return $tableSequenceName === $sequenceName;
108 92
    }
109
110
    public function visit(Visitor $visitor) : void
111
    {
112 92
        $visitor->acceptSequence($this);
113
    }
114
}
115