getPseudoRandomString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maztech\PseudoRandomString;
4
5
use Maztech\Exceptions\InstagramSDKException;
6
7
class RandomBytesPseudoRandomStringGenerator implements PseudoRandomStringGeneratorInterface
8
{
9
    use PseudoRandomStringGeneratorTrait;
10
11
    /**
12
     * @const string The error message when generating the string fails.
13
     */
14
    const ERROR_MESSAGE = 'Unable to generate a cryptographically secure pseudo-random string from random_bytes(). ';
15
16
    /**
17
     * @throws InstagramSDKException
18
     */
19
    public function __construct()
20
    {
21
        if (!function_exists('random_bytes')) {
22
            throw new InstagramSDKException(
23
                static::ERROR_MESSAGE .
24
                'The function random_bytes() does not exist.'
25
            );
26
        }
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function getPseudoRandomString($length)
33
    {
34
        $this->validateLength($length);
35
36
        return $this->binToHex(random_bytes($length), $length);
37
    }
38
}
39