OpenSslTokenGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 0

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 3
cbo 0
dl 0
loc 29
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A generate() 0 4 1
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