McryptPseudoRandomStringGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getPseudoRandomString() 0 14 2
1
<?php
2
3
namespace Maztech\PseudoRandomString;
4
5
use Maztech\Exceptions\InstagramSDKException;
6
7
class McryptPseudoRandomStringGenerator 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 mcrypt_create_iv(). ';
15
16
    /**
17
     * @throws InstagramSDKException
18
     */
19
    public function __construct()
20
    {
21
        if (!function_exists('mcrypt_create_iv')) {
22
            throw new InstagramSDKException(
23
                static::ERROR_MESSAGE .
24
                'The function mcrypt_create_iv() does not exist.'
25
            );
26
        }
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function getPseudoRandomString($length)
33
    {
34
        $this->validateLength($length);
35
36
        $binaryString = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
37
38
        if ($binaryString === false) {
39
            throw new InstagramSDKException(
40
                static::ERROR_MESSAGE .
41
                'mcrypt_create_iv() returned an error.'
42
            );
43
        }
44
45
        return $this->binToHex($binaryString, $length);
46
    }
47
}
48