Passed
Push — master ( 5318fc...c6584f )
by Marcel
02:35
created

SequentialGenerator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UMA\Uuid;
6
7
/**
8
 * An UuidGenerator that returns sequential UUIDs, starting
9
 * from the NIL Uuid and going up. This can be useful in a unit testing
10
 * context, where deterministic and easily readable UUIDs might be a boon.
11
 *
12
 * Note that only the lowest 64 bits of the UUID are incremented. That should
13
 * be enough, though.
14
 *
15
 * Don't use this one in production.
16
 */
17
class SequentialGenerator implements UuidGenerator
18
{
19
    /**
20
     * @var int
21
     */
22
    private $counter;
23
24
    /**
25
     * @var string
26
     */
27
    private $head;
28
29
    /**
30
     * @example $mark = 15 and $start = 10 will generate:
31
     *  '00000000-0000-000f-0000-00000000000a'
32
     *  '00000000-0000-000f-0000-00000000000b'
33
     *  '00000000-0000-000f-0000-00000000000c'
34
     *  ...
35
     */
36 3
    public function __construct(int $mark = 0, int $start = 0)
37
    {
38 3
        $this->head = \pack('J', $mark);
39 3
        $this->counter = $start;
40 3
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function generate(string $name = null): Uuid
46
    {
47 3
        return Uuid::fromBytes($this->head . \pack('J', $this->counter++));
48
    }
49
}
50