Passed
Push — master ( 5990df...7a1d0c )
by Sergei
13:47
created

Sequence   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 14
eloc 29
dl 0
loc 134
ccs 30
cts 36
cp 0.8333
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCache() 0 3 1
A visit() 0 3 1
A getInitialValue() 0 3 1
A getAllocationSize() 0 3 1
A isAutoIncrementsFor() 0 23 4
A setAllocationSize() 0 5 2
A setCache() 0 5 1
A setInitialValue() 0 5 2
A __construct() 0 6 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Schema;
21
22
use Doctrine\DBAL\Schema\Visitor\Visitor;
23
use function count;
24
use function is_numeric;
25
use function sprintf;
26
27
/**
28
 * Sequence structure.
29
 *
30
 * @link   www.doctrine-project.org
31
 * @since  2.0
32
 * @author Benjamin Eberlei <[email protected]>
33
 */
34
class Sequence extends AbstractAsset
35
{
36
    /**
37
     * @var int
38
     */
39
    protected $allocationSize = 1;
40
41
    /**
42
     * @var int
43
     */
44
    protected $initialValue = 1;
45
46
    /**
47
     * @var int|null
48
     */
49
    protected $cache = null;
50
51
    /**
52
     * @param string   $name
53
     * @param int      $allocationSize
54
     * @param int      $initialValue
55
     * @param int|null $cache
56
     */
57 854
    public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null)
58
    {
59 854
        $this->_setName($name);
60 854
        $this->setAllocationSize($allocationSize);
61 854
        $this->setInitialValue($initialValue);
62 854
        $this->cache          = $cache;
63 854
    }
64
65
    /**
66
     * @return int
67
     */
68 503
    public function getAllocationSize()
69
    {
70 503
        return $this->allocationSize;
71
    }
72
73
    /**
74
     * @return int
75
     */
76 484
    public function getInitialValue()
77
    {
78 484
        return $this->initialValue;
79
    }
80
81
    /**
82
     * @return int|null
83
     */
84 360
    public function getCache()
85
    {
86 360
        return $this->cache;
87
    }
88
89
    /**
90
     * @param int $allocationSize
91
     *
92
     * @return \Doctrine\DBAL\Schema\Sequence
93
     */
94 854
    public function setAllocationSize($allocationSize)
95
    {
96 854
        $this->allocationSize = is_numeric($allocationSize) ? (int) $allocationSize : 1;
0 ignored issues
show
introduced by
The condition is_numeric($allocationSize) is always true.
Loading history...
97
98 854
        return $this;
99
    }
100
101
    /**
102
     * @param int $initialValue
103
     *
104
     * @return \Doctrine\DBAL\Schema\Sequence
105
     */
106 854
    public function setInitialValue($initialValue)
107
    {
108 854
        $this->initialValue = is_numeric($initialValue) ? (int) $initialValue : 1;
0 ignored issues
show
introduced by
The condition is_numeric($initialValue) is always true.
Loading history...
109
110 854
        return $this;
111
    }
112
113
    /**
114
     * @param int $cache
115
     *
116
     * @return \Doctrine\DBAL\Schema\Sequence
117
     */
118
    public function setCache($cache)
119
    {
120
        $this->cache = $cache;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Checks if this sequence is an autoincrement sequence for a given table.
127
     *
128
     * This is used inside the comparator to not report sequences as missing,
129
     * when the "from" schema implicitly creates the sequences.
130
     *
131
     * @param \Doctrine\DBAL\Schema\Table $table
132
     *
133
     * @return bool
134
     */
135 76
    public function isAutoIncrementsFor(Table $table)
136
    {
137 76
        if ( ! $table->hasPrimaryKey()) {
138
            return false;
139
        }
140
141 76
        $pkColumns = $table->getPrimaryKey()->getColumns();
142
143 76
        if (count($pkColumns) != 1) {
144
            return false;
145
        }
146
147 76
        $column = $table->getColumn($pkColumns[0]);
148
149 76
        if ( ! $column->getAutoincrement()) {
150
            return false;
151
        }
152
153 76
        $sequenceName      = $this->getShortestName($table->getNamespaceName());
154 76
        $tableName         = $table->getShortestName($table->getNamespaceName());
155 76
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
156
157 76
        return $tableSequenceName === $sequenceName;
158
    }
159
160
    /**
161
     * @param \Doctrine\DBAL\Schema\Visitor\Visitor $visitor
162
     *
163
     * @return void
164
     */
165 76
    public function visit(Visitor $visitor)
166
    {
167 76
        $visitor->acceptSequence($this);
168 76
    }
169
}
170