Completed
Pull Request — master (#1634)
by
unknown
21:44
created

IncrementGenerator::setStartingId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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\ODM\MongoDB\Id;
21
22
use Doctrine\ODM\MongoDB\DocumentManager;
23
24
/**
25
 * IncrementGenerator is responsible for generating auto increment identifiers. It uses
26
 * a collection and generates the next id by using $inc on a field named "current_id".
27
 *
28
 * The 'collection' property determines which collection name is used to store the
29
 * id values. If not specified it defaults to 'doctrine_increment_ids'.
30
 *
31
 * The 'key' property determines the document ID used to store the id values in the
32
 * collection. If not specified it defaults to the name of the collection for the
33
 * document.
34
 *
35
 * @since       1.0
36
 */
37
class IncrementGenerator extends AbstractIdGenerator
38
{
39
    protected $collection = null;
40
    protected $key = null;
41
    protected $startingId = 1;
42
43
    public function setCollection($collection)
44
    {
45
        $this->collection = $collection;
46
    }
47
48
    public function setKey($key)
49
    {
50
        $this->key = $key;
51
    }
52
    
53 8
    public function setStartingId($startingId)
54
    {
55 8
        $this->startingId = $startingId;
56 8
    }
57
58 8
    /** @inheritDoc */
59 8
    public function generate(DocumentManager $dm, $document)
60
    {
61 8
        $className = get_class($document);
62 8
        $db = $dm->getDocumentDatabase($className);
63
64 8
        $coll = $this->collection ?: 'doctrine_increment_ids';
65 8
        $key = $this->key ?: $dm->getDocumentCollection($className)->getName();
66 8
67 8
        $query = array('_id' => $key);
68 8
        $newObj = array('$inc' => array('current_id' => $this->startingId));
69 8
70 8
        $command = array();
71 8
        $command['findandmodify'] = $coll;
72
        $command['query'] = $query;
73
        $command['update'] = $newObj;
74
        $command['upsert'] = true;
75
        $command['new'] = true;
76
        $result = $db->command($command);
77
        return $result['value']['current_id'];
78
    }
79
}
80