NonceGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractAlphaNumericFromBase64EncodedString() 0 3 1
A generate() 0 11 2
A base64EncodedRandomBytes() 0 3 1
1
<?php
2
3
namespace Risan\OAuth1\Request;
4
5
class NonceGenerator implements NonceGeneratorInterface
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10 1
    public function generate($length = 32)
11
    {
12 1
        $nonce = '';
13
14 1
        while (($currentNonceLength = strlen($nonce)) < $length) {
15 1
            $size = $length - $currentNonceLength;
16 1
            $randomString = $this->base64EncodedRandomBytes($size);
17 1
            $nonce .= substr($this->extractAlphaNumericFromBase64EncodedString($randomString), 0, $size);
18
        }
19
20 1
        return $nonce;
21
    }
22
23
    /**
24
     * Get cryptographically secure base64 encoded random bytes.
25
     *
26
     * @param int $length
27
     *
28
     * @return string
29
     */
30 2
    public function base64EncodedRandomBytes($length)
31
    {
32 2
        return base64_encode(random_bytes($length));
33
    }
34
35
    /**
36
     * Extract the alphanumeric characters from base64 encoded string.
37
     *
38
     * @param string $string
39
     *
40
     * @return string
41
     */
42 2
    public function extractAlphaNumericFromBase64EncodedString($string)
43
    {
44 2
        return str_replace(['/', '+', '='], '', $string);
45
    }
46
}
47