PasswordResetRequestForm   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 13 1
A sendEmail() 0 23 4
1
<?php
2
namespace zacksleo\yii2\backend\models\forms;
3
4
use Yii;
5
use yii\base\Model;
6
use zacksleo\yii2\backend\models\Admin;
7
8
/**
9
 * Password reset request form
10
 */
11
class PasswordResetRequestForm extends Model
12
{
13
    public $email;
14
15
16
    /**
17
     * @inheritdoc
18
     */
19 1
    public function rules()
20
    {
21
        return [
22 1
            ['email', 'trim'],
23
            ['email', 'required'],
24
            ['email', 'email'],
25
            ['email', 'exist',
26
                'targetClass' => '\zacksleo\yii2\backend\models\Admin',
27
                'filter' => ['status' => Admin::STATUS_ACTIVE],
28
                'message' => 'There is no user with this email address.'
29
            ],
30
        ];
31
    }
32
33
    /**
34
     * Sends an email with a link, for resetting the password.
35
     *
36
     * @return bool whether the email was send
37
     */
38 1
    public function sendEmail()
39
    {
40
        /* @var $user Admin */
41 1
        $user = Admin::find()->canLogin()->email($this->email)->one();
42
43 1
        if (!$user) {
44
            return false;
45
        }
46
47 1
        if ($user && $user->generatePasswordResetToken(true)) {
48 1
            $params = Yii::$app->params;
49 1
            $mailer = Yii::$app->mailer;
50
            //$mailer->htmlLayout = '@vendor/zacksleo/yii2-backend/mail/layouts/html';
51 1
            return $mailer->compose(
52 1
                ['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'],
53 1
                ['user' => $user])
54 1
                ->setFrom([$params['support.email'] => $params['support.name']])
55 1
                ->setTo($this->email)
56 1
                ->setSubject('Password reset for ' . Yii::$app->name)
57 1
                ->send();
58
        }
59
        return false;
60
    }
61
}
62