TokenGenerator::generate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 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