RestoreForm::restore()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 12
loc 12
c 0
b 0
f 0
rs 9.4285
ccs 0
cts 11
cp 0
cc 3
eloc 7
nc 3
nop 0
crap 12
1
<?php
2
3
namespace inblank\activeuser\models\forms;
4
5
use inblank\activeuser\traits\CommonTrait;
6
use yii;
7
use yii\base\Model;
8
9
class RestoreForm extends Model
10
{
11
    use CommonTrait;
12
13
    const SCENARIO_PASSWORD = 'password';
14
    const SCENARIO_EMAIL = 'email';
15
16
    /**
17
     * @var string user email for restore password
18
     */
19
    public $email;
20
    /**
21
     * @var string new password
22
     */
23
    public $password;
24
25
    /** @inheritdoc */
26
    public function attributeLabels()
27
    {
28
        return [
29
            'email' => Yii::t('activeuser_general', 'Email'),
30
            'password' => Yii::t('activeuser_general', 'Password'),
31
        ];
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function rules()
38
    {
39
        return [
40
            ['email', 'required', 'on' => self::SCENARIO_EMAIL],
41
            ['email', 'email', 'skipOnEmpty' => true],
42
43
            ['password', 'required', 'on' => self::SCENARIO_PASSWORD],
44
            ['password', 'string', 'length' => [6, 20], 'skipOnEmpty' => true],
45
        ];
46
    }
47
48
    /**
49
     * Restore
50
     * @return bool
51
     * @throws yii\base\InvalidConfigException
52
     */
53 View Code Duplication
    public function restore()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        if (!$this->validate()) {
56
            return false;
57
        }
58
        /** @var \inblank\activeuser\models\User $user */
59
        $user = Yii::createObject(self::di('User'))->findOne(['email' => $this->email]);
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
        if ($user) {
61
            $user->restore();
62
        }
63
        return true;
64
    }
65
    /**
66
     * Change password
67
     * @return bool
68
     * @throws yii\base\InvalidConfigException
69
     */
70 View Code Duplication
    public function changePassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        if (!$this->validate()) {
73
            return false;
74
        }
75
        /** @var \inblank\activeuser\models\User $user */
76
        $user = Yii::createObject(self::di('User'))->findOne(['email' => $this->email]);
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        if ($user) {
78
            $user->password = $this->password;
79
            $user->newPassword();
80
        }
81
        return true;
82
    }
83
}
84