Completed
Push — master ( 85769a...b67e6b )
by Igor
07:14
created

SignupForm::sendEmail()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 14
cts 15
cp 0.9333
rs 9.2
cc 3
eloc 14
nc 4
nop 0
crap 3.0026
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use yii\base\Exception;
7
use yii\base\UserException;
8
use app\models\User;
9
use app\services\Tokenizer;
10
11
class SignupForm extends \yii\base\Model
12
{
13
    /**
14
     * @var string
15
     */
16
    public $email;
17
    /**
18
     * @var string
19
     */
20
    public $password;
21
    /**
22
     * @var string
23
     */
24
    public $fullName;
25
    /**
26
     * @var \app\models\User
27
     */
28
    private $user;
29
30
    /**
31
     * @inheritdoc
32
     */
33 20
    public function rules()
34
    {
35
        return [
36 20
            ['fullName', 'required'],
37
            ['fullName', 'string', 'max' => 40],
38
39
            ['password', 'required'],
40
            ['password', 'string', 'min' => 6],
41
42
            ['email', 'filter', 'filter' => 'trim'],
43
            ['email', 'required'],
44
            ['email', 'string', 'max' => 255],
45
            ['email', 'email'],
46 20
            ['email', 'unique',
47 20
                'targetClass' => '\app\models\User',
48 20
                'message' => Yii::t('app.msg', 'This email address has already been taken')
49
            ],
50
        ];
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 19
    public function attributeLabels()
57
    {
58
        return [
59 19
            'email' => Yii::t('app', 'Email'),
60 19
            'password' => Yii::t('app', 'Password'),
61 19
            'fullName' => Yii::t('app', 'Full Name'),
62
        ];
63
    }
64
65
    /**
66
     * Signs user up
67
     *
68
     * @throws Exception
69
     * @return \app\models\User
70
     */
71 3
    public function signup(): User
72
    {
73 3
        $user = new User();
74 3
        $user->email = $this->email;
75 3
        $user->setPassword($this->password);
76 3
        $user->setProfile(['full_name' => $this->fullName]);
77 3
        $user->status = User::STATUS_ACTIVE;
78
79 3
        if (!$user->save()) {
80
            throw new Exception(Yii::t('app.msg', 'An error occurred while saving user'));
81
        }
82
83 3
        $this->user = $user;
84
85 3
        $user->updateDateLogin();
86 3
        Yii::$app->user->login($user, 3600 * 24 * 30);
87
88 3
        $this->sendEmail();
89
90 3
        return $this->user;
91
    }
92
93
    /**
94
     * Sends an email with a link, for confirm the email
95
     *
96
     * @throws UserException
97
     */
98 3
    private function sendEmail(): void
99
    {
100 3
        $tokenizer = new Tokenizer();
101 3
        if (!$tokenizer->validate($this->user->email_confirm_token)) {
102 3
            $this->user->setEmailConfirmToken($tokenizer->generate());
103 3
            $this->user->updateAttributes([
104 3
                'email_confirm_token' => $this->user->email_confirm_token,
105 3
                'date_confirm' => $this->user->date_confirm,
106
            ]);
107
        }
108
109 3
        $sent = Yii::$app->notify->sendMessage(
110 3
            $this->email,
111 3
            Yii::t('app', 'Activate Your Account'),
112 3
            'emailConfirmToken',
113 3
            ['user' => $this->user]
114
        );
115
116 3
        if (!$sent) {
117
            throw new UserException(Yii::t('app.msg', 'An error occurred while sending a message to activate account'));
118
        }
119 3
    }
120
}
121