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

RecoveryCodeCollection::getCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace hiqdev\yii2\mfa\base;
5
6
use Yii;
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 getCodes(): array
22
    {
23
        return $this->codes;
24
    }
25
26
    public function setCount(int $count): self
27
    {
28
        $this->count = $count;
29
30
        return $this;
31
    }
32
33
    public function getCount(): int
34
    {
35
        return $this->count;
36
    }
37
38
    public function setBlocks(int $blocks): self
39
    {
40
        $this->blocks = $blocks;
41
42
        return $this;
43
    }
44
45
    public function getBlocks(): int
46
    {
47
        return $this->blocks;
48
    }
49
50
    public function setBlockLength(int $length): self
51
    {
52
        $this->blockLength = $length;
53
54
        return $this;
55
    }
56
57
    public function getBlockLength(): int
58
    {
59
        return $this->blockLength;
60
    }
61
62
    public function setBlockSeparator(string $separator): self
63
    {
64
        $this->blockSeparator = $separator;
65
66
        return $this;
67
    }
68
69
    public function getBlockSeparator(): string
70
    {
71
        return $this->blockSeparator;
72
    }
73
74
    public function generate(): self
75
    {
76
        $this->reset();
77
        foreach (range(1, $this->getCount()) as $counter) {
78
            $this->codes[] = $this->generateCode();
79
        }
80
81
        return $this;
82
    }
83
84
    public function save(): bool
85
    {
86
        $userId = Yii::$app->user->identity->id;
87
        Recovery::deleteAll(['user_id' => $userId]);
88
89
        $errors = [];
90
        foreach ($this->getCodes() as $code){
91
            $recovery = new Recovery();
92
            if (!$recovery->setUser($userId)->setCode($code)->save()){
93
                $errors[] = $recovery->getErrors();
94
            }
95
        }
96
97
        return empty($errors);
98
    }
99
100
    private function generateCode(): string
101
    {
102
        $codeBlocks = [];
103
        foreach (range(1, $this->getBlocks()) as $counter) {
104
            $codeBlocks[] = $this->generateBlock();
105
        }
106
107
        return implode($this->getBlockSeparator(), $codeBlocks);
108
    }
109
110
    private function generateBlock(): string
111
    {
112
        return bin2hex(random_bytes($this->getBlockLength()));
113
    }
114
115
    private function reset(): self
116
    {
117
        $this->codes = [];
118
119
        return $this;
120
    }
121
122
}