Token   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 42
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateRandomBytes() 0 4 1
A create() 0 8 2
A createFromString() 0 4 1
1
<?php
2
3
namespace Ds\Authenticate;
4
5
/**
6
 * Class Token
7
 * @package Ds\Authenticate
8
 */
9
class Token implements TokenInterface
10
{
11
12
    /**
13
     * Create a random token.
14
     *
15
     * @param  int $length
16
     * @return string
17
     */
18 9
    public function create($length = 40)
19
    {
20 9
        $token = null;
21 9
        while ($token === null) {
22 9
            $token = $this->generateRandomBytes($length);
23
        }
24 9
        return $token;
25
    }
26
27
    /**
28
     * Generate Random byes.
29
     *
30
     * @param  $length
31
     * @return string
32
     */
33 9
    public function generateRandomBytes($length)
34
    {
35 9
        return \openssl_random_pseudo_bytes($length);
36
    }
37
38
    /**
39
     * Create a token from a string + key.
40
     *
41
     * @param  string $string
42
     * @param  string $key
43
     * @param  string $algo
44
     * @return string
45
     */
46 1
    public function createFromString($string, $key, $algo = 'sha256')
47
    {
48 1
        return \bin2hex(\hash_hmac($algo, $string, $key));
49
    }
50
}
51