Completed
Push — master ( 149579...032082 )
by zacksleo
44:40 queued 40:51
created

PasswordResetRequestForm   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 11.76%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 2
cts 17
cp 0.1176
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
B 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 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
    public function sendEmail()
39
    {
40
        /* @var $user Admin */
41
        $user = Admin::find()->canLogin()->email($this->email)->one();
42
43
        if (!$user) {
44
            return false;
45
        }
46
47
        if ($user && $user->generatePasswordResetToken(true)) {
48
            $params = Yii::$app->params;
49
            $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
            return $mailer->compose(
52
                ['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'],
53
                ['user' => $user])
54
                ->setFrom([$params['support.email'] => $params['support.name']])
55
                ->setTo($this->email)
56
                ->setSubject('Password reset for ' . Yii::$app->name)
57
                ->send();
58
        }
59
        return false;
60
    }
61
}
62