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

SequentialGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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