Completed
Push — master ( 2e8683...7e20fc )
by Igor
07:20
created

ConfirmEmailForm::confirmEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use app\models\User;
7
8
class ConfirmEmailForm extends \yii\base\Model
9
{
10
    /**
11
     * @var \app\models\User
12
     */
13
    private $user;
14
15
    /**
16
     * Validate token
17
     *
18
     * @param string $token
19
     * @return boolean
20
     */
21 6
    public function validateToken($token)
22
    {
23 6
        if (empty($token) || !is_string($token)) {
24 2
            return false;
25
        }
26
27 4
        $this->user = User::findByEmailConfirmToken($token);
28
29 4
        if (!$this->user) {
30 2
            return false;
31
        }
32
33 2
        return true;
34
    }
35
36
    /**
37
     * Confirm email
38
     *
39
     * @return boolean
40
     */
41 2
    public function confirmEmail()
42
    {
43 2
        $this->user->setConfirmed();
44 2
        return $this->user->save(false);
45
    }
46
47
    /**
48
     * Sends an email with a link, for confirm the email
49
     *
50
     * @param User $user
51
     * @return boolean
52
     */
53
    public function sendEmail($user)
54
    {
55
        if (!User::isTokenValid($user->email_confirm_token)) {
56
            $user->generateEmailConfirmToken();
57
            $user->updateAttributes([
58
                'email_confirm_token' => $user->email_confirm_token,
59
                'date_confirm' => $user->date_confirm,
60
            ]);
61
        }
62
63
        return Yii::$app->notify->sendMessage(
64
            $user->email,
65
            Yii::t('app', 'Activate Your Account'),
66
            'emailConfirmToken',
67
            ['user' => $user]
68
        );
69
    }
70
}
71