Completed
Push — master ( 032082...7ec744 )
by zacksleo
36:12 queued 34:41
created

PasswordResetRequestForm::sendEmail()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 3
nop 0
dl 0
loc 23
ccs 13
cts 15
cp 0.8667
crap 4.0378
rs 8.7972
c 0
b 0
f 0
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 2
    public function rules()
20
    {
21
        return [
22 2
            ['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 2
    public function sendEmail()
39
    {
40
        /* @var $user Admin */
41 2
        $user = Admin::find()->canLogin()->email($this->email)->one();
42
43 2
        if (!$user) {
44
            return false;
45
        }
46
47 2
        if ($user && $user->generatePasswordResetToken(true)) {
48 2
            $params = Yii::$app->params;
49 2
            $mailer = Yii::$app->mailer;
50
            //$mailer->htmlLayout = '@vendor/zacksleo/yii2-backend/mail/layouts/html';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51 2
            return $mailer->compose(
52 2
                ['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'],
53 2
                ['user' => $user])
54 2
                ->setFrom([$params['support.email'] => $params['support.name']])
55 2
                ->setTo($this->email)
56 2
                ->setSubject('Password reset for ' . Yii::$app->name)
57 2
                ->send();
58
        }
59
        return false;
60
    }
61
}
62