Completed
Push — master ( 0fb455...ba0b4e )
by Hilmi Erdem
03:52 queued 02:19
created

AbstractTokenGenerator::getPlainText()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace Erdemkeren\TemporaryAccess\Token\TokenGenerator;
4
5
use Erdemkeren\TemporaryAccess\Token\GenericToken;
6
7
abstract class AbstractTokenGenerator implements TokenGeneratorInterface
8
{
9
    /**
10
     * The key to be used to encrypt the plain token.
11
     *
12
     * @var string
13
     */
14
    private $key;
15
16
    /**
17
     * TokenGenerator constructor.
18
     *
19
     * @param string $key    The key to be used by the hash algorithm.
20
     * @param int    $length The length of the tokens being generated.
21
     */
22 10
    public function __construct($key, $length = 6)
0 ignored issues
show
Unused Code introduced by
The parameter $length is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24 10
        $this->key = $key;
25 10
    }
26
27
    /**
28
     * Generate a new token.
29
     *
30
     * @param  int $length The length of the plain text to be generated.
31
     *
32
     * @return GenericToken
33
     */
34 4
    public function generate($length = 6)
35
    {
36 4
        $plainText = $this->getPlainText($length);
37
38 4
        return $this->makeToken($this->encrypt($plainText), $plainText);
39
    }
40
41
    /**
42
     * Generate a token from the given plain text.
43
     *
44
     * @param  string $plainText The plain text.
45
     *
46
     * @return GenericToken
47
     */
48 3
    public function fromPlain($plainText)
49
    {
50 3
        return $this->makeToken($this->encrypt($plainText), $plainText);
51
    }
52
53
    /**
54
     * Generate a token from the given encrypted text.
55
     *
56
     * @param  string $encryptedText The encrypted text.
57
     *
58
     * @return GenericToken
59
     */
60 3
    public function fromEncrypted($encryptedText)
61
    {
62 3
        return $this->makeToken($encryptedText);
63
    }
64
65
    /**
66
     * Get the hashed version of the generated token.
67
     *
68
     * @param  string $plainText
69
     *
70
     * @return string
71
     */
72 7
    private function encrypt($plainText)
73
    {
74 7
        return hash_hmac('sha256', $plainText, $this->key);
75
    }
76
77
    /**
78
     * Create a new token instance with the given parameters.
79
     *
80
     * @param  string      $encryptedText
81
     * @param  string|null $plainText
82
     *
83
     * @return GenericToken
84
     */
85 10
    private function makeToken($encryptedText, $plainText = null)
86
    {
87 10
        return new GenericToken($encryptedText, $plainText);
88
    }
89
90
    /**
91
     * Get the plain text version of the token being generated.
92
     *
93
     * @param  int $length
94
     *
95
     * @return string
96
     */
97
    abstract protected function getPlainText($length);
98
}