LoginAttemptBehavior::beforeValidate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
nc 4
nop 0
dl 0
loc 20
c 0
b 0
f 0
cc 4
rs 9.7998
1
<?php
2
3
namespace app\behaviors;
4
5
use Yii;
6
use yii\base\{Model, Behavior};
7
use app\models\LoginAttempt;
8
9
/**
10
 * Class LoginAttemptBehavior
11
 *
12
 * @package app\behaviors
13
 */
14
class LoginAttemptBehavior extends Behavior
15
{
16
    /**
17
     * @var int
18
     */
19
    public $attempts = 3;
20
21
    /**
22
     * @var int
23
     */
24
    public $duration = 300;
25
26
    /**
27
     * @var int
28
     */
29
    public $disableDuration = 900;
30
31
    /**
32
     * @var string
33
     */
34
    public $usernameAttribute = 'login';
35
36
    /**
37
     * @var string
38
     */
39
    public $passwordAttribute = 'password';
40
41
    /**
42
     * @var string
43
     */
44
    public $errorAttribute = 'password';
45
46
    /**
47
     * @var string
48
     */
49
    public $message = 'You have exceeded the password attempts.';
50
51
    /**
52
     * @var LoginAttempt
53
     */
54
    private $_attempt;
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function events()
60
    {
61
        return [
62
            Model::EVENT_BEFORE_VALIDATE => 'beforeValidate',
63
            Model::EVENT_AFTER_VALIDATE => 'afterValidate',
64
        ];
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function beforeValidate()
71
    {
72
        $this->_attempt = LoginAttempt::find()->where([
0 ignored issues
show
Documentation Bug introduced by
It seems like app\models\LoginAttempt:...et_at', time()))->one() can also be of type array. However, the property $_attempt is declared as type app\models\LoginAttempt. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
73
            'key' => $this->key
74
        ])->andWhere([
75
            '>', 'reset_at', time()
76
        ])->one();
77
78
        if ($this->_attempt) {
79
            if ($this->_attempt->amount >= $this->attempts) {
80
                $this->owner->addError($this->errorAttribute, $this->message);
0 ignored issues
show
Bug introduced by
The method addError() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
                $this->owner->/** @scrutinizer ignore-call */ 
81
                              addError($this->errorAttribute, $this->message);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            }
82
83
        } else {
84
            $attempts = LoginAttempt::find()->where([
85
                'key' => $this->key
86
            ])->all();
87
88
            foreach ($attempts as $attempt) {
89
                $attempt->delete();
90
            }
91
        }
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function afterValidate()
98
    {
99
        if ($this->owner->hasErrors($this->passwordAttribute)) {
100
101
            if (!$this->_attempt) {
102
                $this->_attempt = new LoginAttempt;
103
                $this->_attempt->key = $this->key;
104
            }
105
106
            $this->_attempt->amount += 1;
107
108
            if ($this->_attempt->amount >= $this->attempts) {
109
                $this->_attempt->reset_at = time() + $this->disableDuration;
110
            } else {
111
                $this->_attempt->reset_at = time() + $this->duration;
112
            }
113
114
            $this->_attempt->save();
115
        }
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getKey(): string
122
    {
123
        return sha1($this->owner->{$this->usernameAttribute});
124
    }
125
}
126