Cryptography::generateRandomBitsInHex()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Helper;
4
5
final class Cryptography
6
{
7
    /**
8
     * @var string
9
     */
10
    private static $fakeRandomBitsInHex;
11
12
    /**
13
     * @param string $fakeRandomBitsInHex
14
     *
15
     * @return void
16
     */
17 12
    public static function fakeRandomBitsInHex($fakeRandomBitsInHex)
18
    {
19 12
        self::$fakeRandomBitsInHex = $fakeRandomBitsInHex;
20 12
    }
21
22
    /**
23
     * Generate random bits in hexadecimal representation
24
     *
25
     * @param int $bits
26
     *
27
     * @return string
28
     *
29
     * @throws \Exception
30
     */
31 35
    public static function generateRandomBitsInHex($bits)
32
    {
33 35
        if (null !== self::$fakeRandomBitsInHex) {
34 5
            return self::$fakeRandomBitsInHex;
35
        }
36
37 34
        $length = $bits / 8;
38 34
        if ($length < 1) {
39 1
            throw new \Exception('Length must be greater than 0.');
40
        }
41
42 33
        return bin2hex(
43 33
            random_bytes($length)
44 33
        );
45
    }
46
}
47