Completed
Push — master ( c3cfbd...a5cba6 )
by Igor
01:40
created

SignupForm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 122
rs 10
c 0
b 0
f 0

5 Methods

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