TokenFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\Security\Token;
13
14
use DateInterval;
15
use DateTime;
16
17
class TokenFactory implements TokenFactoryInterface
18
{
19
    /**
20
     * @var DateInterval
21
     */
22
    private $ttl;
23
24
    /**
25
     * @var int
26
     */
27
    private $length;
28
29
    public function __construct(int $ttl, int $length = 32)
30
    {
31
        $this->ttl = new DateInterval(sprintf('PT%dS', $ttl));
32
        $this->length = $length;
33
    }
34
35
    public function createToken(): TokenInterface
36
    {
37
        return new Token($this->generateToken(), new DateTime(), $this->ttl);
38
    }
39
40
    private function generateToken(): string
41
    {
42
        return rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
43
    }
44
}
45