Completed
Pull Request — master (#1634)
by
unknown
24:52
created

IncrementGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 71.43%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 58
ccs 15
cts 21
cp 0.7143
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setCollection() 0 4 1
A setKey() 0 4 1
A setStartingId() 0 4 1
B generate() 0 35 5
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
        /*
68 8
         * Unable to use '$inc' and '$setOnInsert' together due to known bug.
69 8
         * @see https://jira.mongodb.org/browse/SERVER-10711
70 8
         * Results in error: Cannot update 'current_id' and 'current_id' at the same time
71 8
         */
72
        $command = array(
73
            'findAndModify' => $coll,
74
            'query' => array('_id' => $key),
75
            'update' => array('$inc' => array('current_id' => 1)),
76
            'new' => true,
77
        );
78
        $result = $db->command($command);
79
80
        /*
81
         * Updated nothing - counter doesn't exist, creating new counter.
82
         */
83
        if(array_key_exists('value', $result) && !isset($result['value'])) {
84
            $command = array(
85
                'insert' => $coll,
86
                'documents' => array(array('_id' => $key, 'current_id' => $this->startingId))
87
            );
88
            $result = $db->command($command);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
            return $this->startingId;
90
        }
91
92
        return $result['value']['current_id'];
93
    }
94
}
95