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

Recovery   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 41
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 9 1
A setUser() 0 5 1
A setCode() 0 5 1
A save() 0 6 1
A hashCode() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace hiqdev\yii2\mfa\base;
5
6
use Yii;
7
use yii\db\ActiveRecord;
8
9
class Recovery extends ActiveRecord
10
{
11
    public static function tableName()
12
    {
13
        return '{{mfa_recovery_codes}}';
14
    }
15
16
    public function rules(): array
17
    {
18
        return [
19
            ['user_id', 'integer'],
20
            ['user_id', 'required'],
21
            ['code', 'string'],
22
            ['code', 'required'],
23
        ];
24
    }
25
26
    public function setUser(int $id): self {
27
        $this->user_id = $id;
28
29
        return $this;
30
    }
31
32
    public function setCode(string $code): self {
33
        $this->code = $code;
34
35
        return $this;
36
    }
37
38
    public function save($runValidation = true, $attributeNames = null): bool
39
    {
40
        $this->code = $this->hashCode($this->code);
41
42
        return parent::save($runValidation, $attributeNames);
43
    }
44
45
    private function hashCode(string $code): string
46
    {
47
        return Yii::$app->security->generatePasswordHash($code);
48
    }
49
}