Completed
Pull Request — master (#13)
by
unknown
13:55
created

RecoveryCodeCollection::getBlockSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace hiqdev\yii2\mfa\base;
5
6
7
use yii\base\BaseObject;
8
9
class RecoveryCodeCollection extends BaseObject
10
{
11
    protected $codes = [];
12
13
    protected $count = 10;
14
15
    protected $blocks = 4;
16
17
    protected $blockLength = 3;
18
19
    protected $blockSeparator = '-';
20
21
    public function setCount(int $count): self
22
    {
23
        $this->count = $count;
24
25
        return $this;
26
    }
27
28
    public function getCount(): int
29
    {
30
        return $this->count;
31
    }
32
33
    public function setBlocks(int $blocks): self
34
    {
35
        $this->blocks = $blocks;
36
37
        return $this;
38
    }
39
40
    public function getBlocks(): int
41
    {
42
        return $this->blocks;
43
    }
44
45
    public function setBlockLength(int $length): self
46
    {
47
        $this->blockLength = $length;
48
49
        return $this;
50
    }
51
52
    public function getBlockLength(): int
53
    {
54
        return $this->blockLength;
55
    }
56
57
    public function setBlockSeparator(string $separator): self
58
    {
59
        $this->blockSeparator = $separator;
60
61
        return $this;
62
    }
63
64
    public function getBlockSeparator(): string
65
    {
66
        return $this->blockSeparator;
67
    }
68
69
    public function generate(): array
70
    {
71
        $this->reset();
72
        foreach (range(1, $this->getCount()) as $counter) {
73
            $this->codes[] = $this->generateCode();
74
        }
75
76
        return $this->codes;
77
    }
78
79
    private function generateCode(): string
80
    {
81
        $codeBlocks = [];
82
        foreach (range(1, $this->getBlocks()) as $counter) {
83
            $codeBlocks[] = $this->generateBlock();
84
        }
85
86
        return implode($this->getBlockSeparator(), $codeBlocks);
87
    }
88
89
    private function generateBlock(): string
90
    {
91
        return bin2hex(random_bytes($this->getBlockLength()));
92
    }
93
94
    private function reset(): self
95
    {
96
        $this->codes = [];
97
98
        return $this;
99
    }
100
101
}