| Total Complexity | 9 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class DbIdGenerator implements IdGeneratorInterface |
||
| 10 | { |
||
| 11 | protected $idBlockSize; |
||
| 12 | protected $nextId; |
||
| 13 | protected $lastId; |
||
| 14 | |||
| 15 | protected $commandExecutor; |
||
| 16 | |||
| 17 | public function __construct() |
||
| 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)); |
||
|
|
|||
| 36 | $this->nextId = $idBlock->getNextId(); |
||
| 37 | $this->lastId = $idBlock->getLastId(); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function getIdBlockSize(): int |
||
| 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 |
||
| 68 | } |
||
| 69 | } |
||
| 70 |