Passed
Pull Request — master (#101)
by Łukasz
03:05
created

TokenFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createToken() 0 3 1
A generateToken() 0 3 1
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
namespace FSi\Bundle\AdminSecurityBundle\Security\Token;
11
12
use DateInterval;
13
use DateTime;
14
15
class TokenFactory implements TokenFactoryInterface
16
{
17
    /**
18
     * @var DateInterval
19
     */
20
    private $ttl;
21
22
    /**
23
     * @var int
24
     */
25
    private $length;
26
27
    /**
28
     * @param integer $ttl
29
     * @param integer $length
30
     */
31
    public function __construct($ttl, $length = 32)
32
    {
33
        $this->ttl = new DateInterval(sprintf('PT%dS', $ttl));
34
        $this->length = $length;
35
    }
36
37
    /**
38
     * @return TokenInterface
39
     */
40
    public function createToken()
41
    {
42
        return new Token($this->generateToken(), new DateTime(), $this->ttl);
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    private function generateToken()
49
    {
50
        return rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
51
    }
52
}
53