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

Recovery::setUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
}