Completed
Push — master ( 09e072...c7757e )
by Luís
15s
created

Sequence   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 134
ccs 27
cts 36
cp 0.75
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B isAutoIncrementsFor() 0 23 4
A setInitialValue() 0 5 2
A getCache() 0 3 1
A setAllocationSize() 0 5 2
A visit() 0 3 1
A getInitialValue() 0 3 1
A getAllocationSize() 0 3 1
A __construct() 0 6 3
A setCache() 0 5 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
24
/**
25
 * Sequence structure.
26
 *
27
 * @link   www.doctrine-project.org
28
 * @since  2.0
29
 * @author Benjamin Eberlei <[email protected]>
30
 */
31
class Sequence extends AbstractAsset
32
{
33
    /**
34
     * @var integer
35
     */
36
    protected $allocationSize = 1;
37
38
    /**
39
     * @var integer
40
     */
41
    protected $initialValue = 1;
42
43
    /**
44
     * @var integer|null
45
     */
46
    protected $cache = null;
47
48
    /**
49
     * @param string       $name
50
     * @param integer      $allocationSize
51
     * @param integer      $initialValue
52
     * @param integer|null $cache
53
     */
54 40
    public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null)
55
    {
56 40
        $this->_setName($name);
57 40
        $this->allocationSize = is_numeric($allocationSize) ? $allocationSize : 1;
58 40
        $this->initialValue = is_numeric($initialValue) ? $initialValue : 1;
59 40
        $this->cache = $cache;
60 40
    }
61
62
    /**
63
     * @return integer
64
     */
65 21
    public function getAllocationSize()
66
    {
67 21
        return $this->allocationSize;
68
    }
69
70
    /**
71
     * @return integer
72
     */
73 20
    public function getInitialValue()
74
    {
75 20
        return $this->initialValue;
76
    }
77
78
    /**
79
     * @return integer|null
80
     */
81 14
    public function getCache()
82
    {
83 14
        return $this->cache;
84
    }
85
86
    /**
87
     * @param integer $allocationSize
88
     *
89
     * @return \Doctrine\DBAL\Schema\Sequence
90
     */
91 1
    public function setAllocationSize($allocationSize)
92
    {
93 1
        $this->allocationSize = is_numeric($allocationSize) ? $allocationSize : 1;
94
95 1
        return $this;
96
    }
97
98
    /**
99
     * @param integer $initialValue
100
     *
101
     * @return \Doctrine\DBAL\Schema\Sequence
102
     */
103
    public function setInitialValue($initialValue)
104
    {
105
        $this->initialValue = is_numeric($initialValue) ? $initialValue : 1;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param integer $cache
112
     *
113
     * @return \Doctrine\DBAL\Schema\Sequence
114
     */
115
    public function setCache($cache)
116
    {
117
        $this->cache = $cache;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Checks if this sequence is an autoincrement sequence for a given table.
124
     *
125
     * This is used inside the comparator to not report sequences as missing,
126
     * when the "from" schema implicitly creates the sequences.
127
     *
128
     * @param \Doctrine\DBAL\Schema\Table $table
129
     *
130
     * @return boolean
131
     */
132 4
    public function isAutoIncrementsFor(Table $table)
133
    {
134 4
        if ( ! $table->hasPrimaryKey()) {
135
            return false;
136
        }
137
138 4
        $pkColumns = $table->getPrimaryKey()->getColumns();
139
140 4
        if (count($pkColumns) != 1) {
141
            return false;
142
        }
143
144 4
        $column = $table->getColumn($pkColumns[0]);
145
146 4
        if ( ! $column->getAutoincrement()) {
147
            return false;
148
        }
149
150 4
        $sequenceName      = $this->getShortestName($table->getNamespaceName());
151 4
        $tableName         = $table->getShortestName($table->getNamespaceName());
152 4
        $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
153
154 4
        return $tableSequenceName === $sequenceName;
155
    }
156
157
    /**
158
     * @param \Doctrine\DBAL\Schema\Visitor\Visitor $visitor
159
     *
160
     * @return void
161
     */
162 4
    public function visit(Visitor $visitor)
163
    {
164 4
        $visitor->acceptSequence($this);
165 4
    }
166
}
167