ConfirmationTokenGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 22
rs 10
c 2
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getToken() 0 5 1
A __construct() 0 7 2
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Services\EmailConfirmation;
20
21
use InvalidArgumentException;
22
23
/**
24
 * A service to generate a verification token.
25
 */
26
class ConfirmationTokenGenerator
27
{
28
    private $bytes_length;
29
30
    public function __construct(int $bytes_length = 16)
31
    {
32
        if ($bytes_length < 10) {
33
            throw new InvalidArgumentException('$bytes_length must be greater than 10 to be secure!');
34
        }
35
36
        $this->bytes_length = $bytes_length;
37
    }
38
39
    /**
40
     * Returns a truly random token with a configured length.
41
     * It returns a hex encoded 16 random bytes.
42
     */
43
    public function getToken(): string
44
    {
45
        $bytes = random_bytes($this->bytes_length);
46
47
        return bin2hex($bytes);
48
    }
49
}
50