Completed
Push — master ( 4b5cac...cb75bf )
by Charles
03:12
created

ResetPasswordAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B post() 0 35 5
1
<?php
2
3
namespace yrc\api\actions;
4
5
use app\forms\ResetPassword;
6
use yrc\rest\Action as RestAction;
7
8
use yii\web\HttpException;
9
use Yii;
10
11
/**
12
 * @class ResetPasswordAction
13
 * Handles token refresh
14
 */
15
class ResetPasswordAction extends RestAction
16
{
17
    /**
18
     * [POST] /api/[...]/reset_password
19
     * Allows a user to reset their password
20
     * @return mixed
21
     */
22
    public static function post($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        static $form;
25
        $token = Yii::$app->request->post('reset_token', false);
26
27
        // Determine the correct scenario to use based upon the reset token
28
        if ($token === false) {
29
            $form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_INIT]);
30
        } else {
31
            $form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_RESET]);
32
        }
33
        
34
        // Load the form
35
        if ($form->load(['ResetPassword' => Yii::$app->request->post()])) {
36
            $form->password = Yii::$app->request->post('password', null);
37
            $form->password_verify = Yii::$app->request->post('password_verify', null);
38
39
            // If the user is authenticated, populate the model
40
            if (!Yii::$app->user->isGuest) {
41
                $user = Yii::$app->yrc->userClass::findOne(['id' => Yii::$app->user->id]);
42
                $form->setUser($user);
43
            } else {
44
                $form->email = Yii::$app->request->post('email', null);
45
            }
46
47
            // Validate the form and make sure all of the attributes are set, then perform the reset task depending upon the scenario
48
            if ($form->validate()) {
49
                return $form->reset();
50
            }
51
52
            throw new HttpException(400, \json_encode($form->getErrors()));
53
        }
54
            
55
        return false;
56
    }
57
}