Completed
Pull Request — master (#91)
by Piotr
02:36
created

TokenFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1
rs 10

3 Methods

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