TimestampFirstCombGenerator::generate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace PSB\Core\UuidGeneration\Comb;
3
4
5
use PSB\Core\UuidGeneration\UuidGeneratorInterface;
6
7
class TimestampFirstCombGenerator implements UuidGeneratorInterface
8
{
9
    /**
10
     * @return string
11
     */
12 2
    public function generate()
13
    {
14 2
        $time = explode(' ', microtime(false));
15 2
        $seconds = (int)$time[1];
16 2
        $milliseconds = (int)substr($time[0], 2, 4);
17
18
        $params = [
19
            // 32 bits for "time_low"
20 2
            $seconds >> 16,
21 2
            $seconds & 0xffff,
22
            // 16 bits for "time_mid"
23 2
            $milliseconds,
24
            // 16 bits for "time_hi_and_version",
25
            // four most significant bits holds version number 4
26 2
            mt_rand(0, 0x0fff) | 0x4000,
27
            // 16 bits, 8 bits for "clock_seq_hi_res",
28
            // 8 bits for "clock_seq_low",
29
            // two most significant bits hold one and zero for variant DCE1.1
30 2
            mt_rand(0, 0x3fff) | 0x8000,
31
            // 48 bits for "node"
32 2
            mt_rand(0, 0xffff),
33 2
            mt_rand(0, 0xffff),
34 2
            mt_rand(0, 0xffff)
35
        ];
36
37 2
        return vsprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', $params);
38
    }
39
}
40