ResetPasswordAction::run()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 14
nc 4
nop 1
1
<?php
2
3
namespace yiicod\auth\actions\webUser;
4
5
use Yii;
6
use yii\base\Action;
7
use yii\base\Event;
8
use yii\base\InvalidParamException;
9
use yiicod\auth\events\ResetPasswordErrorEvent;
10
use yiicod\auth\events\ResetPasswordEvent;
11
12
class ResetPasswordAction extends Action
13
{
14
    const EVENT_BEFORE_RESET_PASSWORD = 'beforeResetPassword';
15
    const EVENT_AFTER_RESET_PASSWORD = 'afterResetPassword';
16
    const EVENT_ERROR_RESET_PASSWORD = 'errorResetPassword';
17
18
    public $view = '@yiicod/yii2-auth/views/webUser/resetPassword';
19
20
    public function run($token)
21
    {
22
        $resetPasswordFormClass = Yii::$app->get('auth')->modelMap['resetPasswordForm']['class'];
23
24
        Event::trigger(self::class, static::EVENT_BEFORE_RESET_PASSWORD, new ResetPasswordEvent($this, $token, ['sender' => $this]));
25
        try {
26
            $model = new $resetPasswordFormClass($token);
27
        } catch (InvalidParamException $e) {
28
            Event::trigger(self::class, static::EVENT_ERROR_RESET_PASSWORD, new ResetPasswordErrorEvent($this, $token, $e, ['sender' => $this]));
29
        }
30
31
        if ($model instanceof $resetPasswordFormClass &&
32
            $model->load(Yii::$app->request->post()) &&
33
            $model->validate() &&
34
            $model->resetPassword()
35
        ) {
36
            Event::trigger(self::class, static::EVENT_AFTER_RESET_PASSWORD, new ResetPasswordEvent($this, $model, ['sender' => $this]));
37
        }
38
39
        return $this->controller->render($this->view, [
40
            'model' => $model,
41
        ]);
42
    }
43
}
44