Failed Conditions
Pull Request — develop (#3348)
by Sergei
22:47
created

Sequence::setInitialValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 974
    public function __construct(string $name, int $allocationSize = 1, int $initialValue = 1, ?int $cache = null)
28
    {
29 974
        $this->_setName($name);
30 974
        $this->setAllocationSize($allocationSize);
31 974
        $this->setInitialValue($initialValue);
32 974
        $this->cache = $cache;
33 974
    }
34
35 971
    public function getName() : string
36
    {
37 971
        $name = parent::getName();
38 971
        assert(is_string($name));
39
40 971
        return $name;
41
    }
42
43 955
    public function getAllocationSize() : int
44
    {
45 955
        return $this->allocationSize;
46
    }
47
48 954
    public function getInitialValue() : int
49
    {
50 954
        return $this->initialValue;
51
    }
52
53 937
    public function getCache() : ?int
54
    {
55 937
        return $this->cache;
56
    }
57
58 974
    public function setAllocationSize(int $allocationSize) : self
59
    {
60 974
        $this->allocationSize = $allocationSize;
61
62 974
        return $this;
63
    }
64
65 974
    public function setInitialValue(int $initialValue) : self
66
    {
67 974
        $this->initialValue = $initialValue;
68
69 974
        return $this;
70
    }
71
72
    public function setCache(int $cache) : self
73
    {
74
        $this->cache = $cache;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Checks if this sequence is an autoincrement sequence for a given table.
81
     *
82
     * This is used inside the comparator to not report sequences as missing,
83
     * when the "from" schema implicitly creates the sequences.
84
     */
85 412
    public function isAutoIncrementsFor(Table $table) : bool
86
    {
87 412
        $primaryKey = $table->getPrimaryKey();
88
89 412
        if ($primaryKey === null) {
90
            return false;
91
        }
92
93 412
        $pkColumns = $primaryKey->getColumns();
94
95 412
        if (count($pkColumns) !== 1) {
96
            return false;
97
        }
98
99 412
        $column = $table->getColumn($pkColumns[0]);
100
101 412
        if (! $column->getAutoincrement()) {
102
            return false;
103
        }
104
105 412
        $sequenceName      = $this->getShortestName($table->getNamespaceName());
106 412
        $tableName         = $table->getShortestName($table->getNamespaceName());
107 412
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
108
109 412
        return $tableSequenceName === $sequenceName;
110
    }
111
112 148
    public function visit(Visitor $visitor) : void
113
    {
114 148
        $visitor->acceptSequence($this);
115 148
    }
116
}
117