Completed
Push — master ( e4197d...f733ed )
by Igor
11:05 queued 09:33
created

SignupForm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 98
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 19 1
A attributeLabels() 0 4 1
A signup() 0 18 4
A sendEmail() 0 18 2
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use app\models\User;
7
use app\models\UserProfile;
8
use app\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\User
26
     */
27
    private $user;
28
29
    /**
30
     * @inheritdoc
31
     */
32 20
    public function rules()
33
    {
34
        return [
35 20
            ['fullName', 'required'],
36
            ['fullName', 'string', 'max' => 40],
37
38
            ['password', 'required'],
39
            ['password', 'string', 'min' => 6],
40
41
            ['email', 'filter', 'filter' => 'trim'],
42
            ['email', 'required'],
43
            ['email', 'string', 'max' => 255],
44
            ['email', 'email'],
45 20
            ['email', 'unique',
46 20
                'targetClass' => '\app\models\User',
47 20
                'message' => Yii::t('app.validators', 'This email address has already been taken')
48
            ],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 19
    public function attributeLabels()
56
    {
57 19
        return array_merge((new User())->attributeLabels(), ['fullName' => Yii::t('app', 'Full Name')]);
58
    }
59
60
    /**
61
     * Signs user up
62
     *
63
     * @return \app\models\User
64
     */
65 16
    public function signup(): ?User
66
    {
67 16
        if ($this->validate()) {
68 3
            $this->user = new User();
69 3
            $this->user->email = $this->email;
70 3
            $this->user->setPassword($this->password);
71 3
            $this->user->setProfile(['full_name' => $this->fullName]);
72 3
            if ($this->user->save()) {
73 3
                $this->user->updateDateLogin();
74
75 3
                if (Yii::$app->user->login($this->user, 3600 * 24 * 30)) {
76 3
                    return $this->user;
77
                }
78
            } // @codeCoverageIgnore
79
        } // @codeCoverageIgnore
80
81 13
        return null;
82
    }
83
84
    /**
85
     * Sends an email with a link, for confirm the email
86
     *
87
     * @return boolean
88
     */
89 3
    public function sendEmail(): bool
90
    {
91 3
        $tokenizer = new Tokenizer();
92 3
        if (!$tokenizer->validate($this->user->email_confirm_token)) {
93 3
            $this->user->setEmailConfirmToken($tokenizer->generate());
94 3
            $this->user->updateAttributes([
95 3
                'email_confirm_token' => $this->user->email_confirm_token,
96 3
                'date_confirm' => $this->user->date_confirm,
97
            ]);
98
        }
99
100 3
        return Yii::$app->notify->sendMessage(
101 3
            $this->email,
102 3
            Yii::t('app', 'Activate Your Account'),
103 3
            'emailConfirmToken',
104 3
            ['user' => $this->user]
105
        );
106
    }
107
}
108