Failed Conditions
Push — master ( 30b923...92920e )
by Marco
19s queued 13s
created

Sequence   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 83.78%

Importance

Changes 0
Metric Value
wmc 14
eloc 30
dl 0
loc 126
ccs 31
cts 37
cp 0.8378
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setInitialValue() 0 5 2
A getCache() 0 3 1
A setAllocationSize() 0 5 2
A getInitialValue() 0 3 1
A getAllocationSize() 0 3 1
A __construct() 0 6 1
A setCache() 0 5 1
A visit() 0 3 1
A isAutoIncrementsFor() 0 25 4
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Schema\Visitor\Visitor;
6
use function count;
7
use function sprintf;
8
9
/**
10
 * Sequence structure.
11
 */
12
class Sequence extends AbstractAsset
13
{
14
    /** @var int */
15
    protected $allocationSize = 1;
16
17
    /** @var int */
18
    protected $initialValue = 1;
19
20
    /** @var int|null */
21
    protected $cache = null;
22
23
    /**
24
     * @param string   $name
25
     * @param int      $allocationSize
26
     * @param int      $initialValue
27
     * @param int|null $cache
28
     */
29 1211
    public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null)
30
    {
31 1211
        $this->_setName($name);
32 1211
        $this->setAllocationSize($allocationSize);
33 1211
        $this->setInitialValue($initialValue);
34 1211
        $this->cache = $cache;
35 1211
    }
36
37
    /**
38
     * @return int
39
     */
40 714
    public function getAllocationSize()
41
    {
42 714
        return $this->allocationSize;
43
    }
44
45
    /**
46
     * @return int
47
     */
48 687
    public function getInitialValue()
49
    {
50 687
        return $this->initialValue;
51
    }
52
53
    /**
54
     * @return int|null
55
     */
56 515
    public function getCache()
57
    {
58 515
        return $this->cache;
59
    }
60
61
    /**
62
     * @param int $allocationSize
63
     *
64
     * @return \Doctrine\DBAL\Schema\Sequence
65
     */
66 1211
    public function setAllocationSize($allocationSize)
67
    {
68 1211
        $this->allocationSize = (int) $allocationSize ?: 1;
69
70 1211
        return $this;
71
    }
72
73
    /**
74
     * @param int $initialValue
75
     *
76
     * @return \Doctrine\DBAL\Schema\Sequence
77
     */
78 1211
    public function setInitialValue($initialValue)
79
    {
80 1211
        $this->initialValue = (int) $initialValue ?: 1;
81
82 1211
        return $this;
83
    }
84
85
    /**
86
     * @param int $cache
87
     *
88
     * @return \Doctrine\DBAL\Schema\Sequence
89
     */
90
    public function setCache($cache)
91
    {
92
        $this->cache = $cache;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Checks if this sequence is an autoincrement sequence for a given table.
99
     *
100
     * This is used inside the comparator to not report sequences as missing,
101
     * when the "from" schema implicitly creates the sequences.
102
     *
103
     * @return bool
104
     */
105 108
    public function isAutoIncrementsFor(Table $table)
106
    {
107 108
        $primaryKey = $table->getPrimaryKey();
108
109 108
        if ($primaryKey === null) {
110
            return false;
111
        }
112
113 108
        $pkColumns = $primaryKey->getColumns();
114
115 108
        if (count($pkColumns) !== 1) {
116
            return false;
117
        }
118
119 108
        $column = $table->getColumn($pkColumns[0]);
120
121 108
        if (! $column->getAutoincrement()) {
122
            return false;
123
        }
124
125 108
        $sequenceName      = $this->getShortestName($table->getNamespaceName());
126 108
        $tableName         = $table->getShortestName($table->getNamespaceName());
127 108
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
128
129 108
        return $tableSequenceName === $sequenceName;
130
    }
131
132
    /**
133
     * @return void
134
     */
135 108
    public function visit(Visitor $visitor)
136
    {
137 108
        $visitor->acceptSequence($this);
138 108
    }
139
}
140