Completed
Push — master ( 44676f...b7bc18 )
by Jens
13:18
created

Uuid::active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Commercetools\Core\Helper;
4
5
class Uuid
6
{
7
    /**
8
     * return bool
9
     */
10 104
    public static function active()
11
    {
12 104
        return function_exists('random_bytes');
13
    }
14
15
    /**
16
     * Return a UUID (version 4) using random bytes
17
     * Note that version 4 follows the format:
18
     *     xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
19
     * where y is one of: [8, 9, A, B]
20
     *
21
     * We use (random_bytes(1) & 0x0F) | 0x40 to force
22
     * the first character of hex value to always be 4
23
     * in the appropriate position.
24
     *
25
     * For 4: http://3v4l.org/q2JN9
26
     * For Y: http://3v4l.org/EsGSU
27
     * For the whole shebang: http://3v4l.org/BkjBc
28
     *
29
     * @link https://paragonie.com/b/JvICXzh_jhLyt4y3
30
     * @link https://creativecommons.org/licenses/by-sa/4.0/
31
     *
32
     * @return string
33
     */
34 530
    public static function uuidv4()
35
    {
36 530
        return implode('-', [
37 530
            bin2hex(random_bytes(4)),
38 530
            bin2hex(random_bytes(2)),
39 530
            bin2hex(chr((ord(random_bytes(1)) & 0x0F) | 0x40)) . bin2hex(random_bytes(1)),
40 530
            bin2hex(chr((ord(random_bytes(1)) & 0x3F) | 0x80)) . bin2hex(random_bytes(1)),
41 530
            bin2hex(random_bytes(6))
42
        ]);
43
    }
44
}
45