Completed
Push — master ( 5e7707...6e3d76 )
by Igor
03:56
created

ConfirmEmailForm::confirmEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use app\models\User;
7
use app\services\Tokenizer;
8
9
class ConfirmEmailForm extends \yii\base\Model
10
{
11
    /**
12
     * @var \app\models\User
13
     */
14
    private $user;
15
16
    /**
17
     * Validate token
18
     *
19
     * @param string $token
20
     * @return boolean
21
     */
22
    public function validateToken(string $token): bool
23
    {
24
        $tokenizer = new Tokenizer();
25
        if (empty($token) || !is_string($token) || !$tokenizer->validate($token)) {
26
            return false;
27
        }
28
29
        $this->user = User::find()->emailConfirmToken($token)->one();
0 ignored issues
show
Documentation Bug introduced by
It seems like \app\models\User::find()...irmToken($token)->one() can also be of type object<yii\db\ActiveRecord> or array. However, the property $user is declared as type object<app\models\User>. 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...
30
31
        if (!$this->user) {
32
            return false;
33
        }
34
35
        return true;
36
    }
37
38
    /**
39
     * Confirm email
40
     *
41
     * @return boolean
42
     */
43
    public function confirmEmail(): bool
44
    {
45
        $this->user->setConfirmed();
46
        return $this->user->save(false);
47
    }
48
49
    /**
50
     * Sends an email with a link, for confirm the email
51
     *
52
     * @param User $user
53
     * @return boolean
54
     */
55
    public function sendEmail(User $user): bool
56
    {
57
        $tokenizer = new Tokenizer();
58
        if (!$tokenizer->validate($user->email_confirm_token)) {
59
            $user->setEmailConfirmToken($tokenizer->generate());
60
            $user->updateAttributes([
61
                'email_confirm_token' => $user->email_confirm_token,
62
                'date_confirm' => $user->date_confirm,
63
            ]);
64
        }
65
66
        return Yii::$app->notify->sendMessage(
67
            $user->email,
68
            Yii::t('app', 'Activate Your Account'),
69
            'emailConfirmToken',
70
            ['user' => $user]
71
        );
72
    }
73
}
74