Test Failed
Push — main ( c7561b...fa446d )
by Bingo
15:52
created

DbIdGenerator::getNextId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Jabe\Engine\Impl\Db;
4
5
use Jabe\Engine\Impl\Cfg\IdGeneratorInterface;
6
use Jabe\Engine\Impl\Cmd\GetNextIdBlockCmd;
7
use Jabe\Engine\Impl\Interceptor\CommandExecutorInterface;
8
9
class DbIdGenerator implements IdGeneratorInterface
10
{
11
    protected $idBlockSize;
12
    protected $nextId;
13
    protected $lastId;
14
15
    protected $commandExecutor;
16
17
    public function __construct()
18
    {
19
        $this->reset();
20
    }
21
22
    public function getNextId(): string
23
    {
24
        if ($this->lastId < $this->nextId) {
25
            $this->getNewBlock();
26
        }
27
        $this->nextId += 1;
28
        $_nextId = $this->nextId;
29
        return strval($_nextId);
30
    }
31
32
    protected function getNewBlock(): void
33
    {
34
        // TODO http://jira.codehaus.org/browse/ACT-45 use a separate 'requiresNew' command executor
35
        $idBlock = $this->commandExecutor->execute(new GetNextIdBlockCmd($idBlockSize));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $idBlockSize does not exist. Did you maybe mean $idBlock?
Loading history...
36
        $this->nextId = $idBlock->getNextId();
37
        $this->lastId = $idBlock->getLastId();
38
    }
39
40
    public function getIdBlockSize(): int
41
    {
42
        return $this->idBlockSize;
43
    }
44
45
    public function setIdBlockSize(int $idBlockSize): void
46
    {
47
        $this->idBlockSize = $idBlockSize;
48
    }
49
50
    public function getCommandExecutor(): CommandExecutorInterface
51
    {
52
        return $this->commandExecutor;
53
    }
54
55
    public function setCommandExecutor(CommandExecutorInterface $commandExecutor): void
56
    {
57
        $this->commandExecutor = $commandExecutor;
58
    }
59
60
    /**
61
     * Reset inner state so that the generator fetches a new block of IDs from the database
62
     * when the next ID generation request is received.
63
     */
64
    public function reset(): void
65
    {
66
        $this->nextId = 0;
67
        $this->lastId = -1;
68
    }
69
}
70