TokenGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 20
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 14 4
1
<?php
2
/**
3
 * Project: token-generator
4
 * Created by: ngdo on 01/09/18
5
 */
6
declare(strict_types=1);
7
8
namespace BSP\TokenGenerator;
9
10
use DateInterval;
11
use Ramsey\Uuid\Uuid;
12
13
class TokenGenerator
14
{
15
    private const RANDOM_BYTES_LENGTH = 12;
16
    private const DURATION = 15;
17
18 5
    public static function generate(DateInterval $duration = null, int $length = null): Token
19
    {
20 5
        if (empty($length) || $length > 105) {
21 3
            $length = self::RANDOM_BYTES_LENGTH;
22
        }
23
24 5
        $duration = !empty($duration) ? $duration : new DateInterval(sprintf('P%1$sD', self::DURATION));
25
26 5
        $random = random_bytes($length);
27
28 5
        $token = base64_encode(sprintf('%1$s%2$s', $random, Uuid::uuid4()->toString()));
29
30 5
        return new Token($token, $duration);
31
    }
32
}
33