Completed
Push — master ( 4f4774...00e15e )
by Dmitry
02:49
created

Sequence   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateKey() 0 10 3
B generateValue() 0 29 4
1
<?php
2
3
namespace Tarantool\Mapper\Plugin;
4
5
use Tarantool\Mapper\Entity;
6
use Tarantool\Mapper\Plugin;
7
use Tarantool\Mapper\Space;
8
9
class Sequence extends Plugin
10
{
11
    public function generateKey(Entity $instance, Space $space)
12
    {
13
        $primary = $space->getPrimaryIndex();
14
        if (count($primary->parts) == 1) {
15
            $key = $space->getFormat()[$primary->parts[0][0]]['name'];
16
            if (!property_exists($instance, $key)) {
17
                $instance->$key = $this->generateValue($space);
18
            }
19
        }
20
    }
21
22
    private function generateValue($space)
23
    {
24
        $spaceId = $space->getId();
0 ignored issues
show
Unused Code introduced by
$spaceId 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...
25
26
        if (!$this->mapper->getSchema()->hasSpace('sequence')) {
27
            $sequence = $this->mapper->getSchema()->createSpace('sequence');
28
            $sequence->addProperty('space', 'unsigned');
29
            $sequence->addProperty('counter', 'unsigned');
30
            $sequence->createIndex('space');
31
        }
32
33
        $entity = $this->mapper->findOne('sequence', $space->getId());
34
        if (!$entity) {
35
            $query = "return box.space.".$space->getName().".index[0]:max()";
36
            $data = $this->mapper->getClient()->evaluate($query)->getData();
37
            $max = $data ? $data[0][0] : 0;
38
39
            $entity = $this->mapper->create('sequence', [
40
                'space' => $space->getId(),
41
                'counter' => $max,
42
            ]);
43
        }
44
45
        $this->mapper->getSchema()->getSpace('sequence')
46
            ->getRepository()
47
            ->update($entity, [['+', 'counter', 1]]);
48
49
        return $entity->counter;
50
    }
51
}
52