OpenSslTokenGenerator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
rs 10
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Generator;
4
5
use LogicException;
6
7
/**
8
 * This token generator is using `openssl` extension to generate random token values.
9
 *
10
 * @author Yann Eugoné <[email protected]>
11
 */
12
class OpenSslTokenGenerator implements TokenGeneratorInterface
13
{
14
    const DEFAULT_LENGTH = 32;
15
16
    /**
17
     * @var int
18
     */
19
    private $length;
20
21
    /**
22
     * @param int $length Token length
23
     */
24 2
    public function __construct($length = self::DEFAULT_LENGTH)
25
    {
26 2
        if (!function_exists('openssl_random_pseudo_bytes')) {
27
            throw new LogicException('The extension "openssl" is required to use "open ssl" token generator.');
28
        }
29
30 2
        $this->length = $length;
31 2
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 1
    public function generate()
37
    {
38 1
        return rtrim(strtr(base64_encode(openssl_random_pseudo_bytes($this->length)), '+/', '-_'), '=');
39
    }
40
}
41