Completed
Push — master ( ee995a...0c9edb )
by Razon
02:23
created

ResendVerificationEmailForm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 31
c 1
b 0
f 0
dl 0
loc 70
ccs 0
cts 47
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateEmail() 0 10 3
A getUser() 0 7 2
A rules() 0 7 1
A sendEmail() 0 20 2
1
<?php
2
namespace App\Http\Form;
3
4
use App\Model\User;
5
use Yii;
6
use yii\base\Model;
7
8
class ResendVerificationEmailForm extends Model
9
{
10
    /**
11
     * @var string
12
     */
13
    public $email;
14
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function rules()
20
    {
21
        return [
22
            ['email', 'trim'],
23
            ['email', 'required'],
24
            ['email', 'email'],
25
            ['email', 'validateEmail'],
26
        ];
27
    }
28
29
    public function validateEmail($attribute)
30
    {
31
        $user = $this->getUser();
32
        if (!$user) {
33
            $this->addError($attribute, 'There is no user with this email address.');
34
            return;
35
        }
36
        if ($user->isActive()) {
37
            $this->addError($attribute, 'The email was verified.');
38
            return;
39
        }
40
    }
41
42
    /**
43
     * Sends confirmation email to user
44
     *
45
     * @return bool whether the email was sent
46
     */
47
    public function sendEmail()
48
    {
49
        $user = User::findOne([
50
            'email' => $this->email,
51
        ]);
52
53
        if ($user === null) {
54
            return false;
55
        }
56
57
        return Yii::$app
58
            ->mailer
59
            ->compose(
60
                ['html' => 'emailVerify-html', 'text' => 'emailVerify-text'],
61
                ['user' => $user]
62
            )
63
            ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
64
            ->setTo($this->email)
65
            ->setSubject('Account registration at ' . Yii::$app->name)
66
            ->send();
67
    }
68
69
    private $user;
70
71
    public function getUser():? User
72
    {
73
        if ($this->user === null) {
74
            $this->user = User::findOne(['email' => $this->email]);
75
        }
76
77
        return $this->user;
78
    }
79
}
80