RandomGenerator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A uuid() 0 7 1
A randFloat() 0 3 1
1
<?php
2
3
namespace bSecure\Util;
4
5
/**
6
 * A basic random generator. This is in a separate class so we the generator
7
 * can be injected as a dependency and replaced with a mock in tests.
8
 */
9
class RandomGenerator
10
{
11
    /**
12
     * Returns a random value between 0 and $max.
13
     *
14
     * @param float $max (optional)
15
     *
16
     * @return float
17
     */
18
    public function randFloat($max = 1.0)
19
    {
20
        return \mt_rand() / \mt_getrandmax() * $max;
21
    }
22
23
    /**
24
     * Returns a v4 UUID.
25
     *
26
     * @return string
27
     */
28
    public function uuid()
29
    {
30
        $arr = \array_values(\unpack('N1a/n4b/N1c', \openssl_random_pseudo_bytes(16)));
31
        $arr[2] = ($arr[2] & 0x0fff) | 0x4000;
32
        $arr[3] = ($arr[3] & 0x3fff) | 0x8000;
33
34
        return \vsprintf('%08x-%04x-%04x-%04x-%04x%08x', $arr);
35
    }
36
}