PasswordResetRequestForm::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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