Completed
Push — master ( c5ccd5...5d3399 )
by Charles
02:37
created

ResetPasswordAction::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
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
     * Allows a user to reset their password
19
     * @return mixed
20
     */
21
    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...
22
    {
23
        static $form;
24
        $token = Yii::$app->request->get('reset_token', false);
25
26
        // Determine the correct scenario to use based upon the reset token
27
        if ($token === false) {
28
            $form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_INIT]);
29
        } else {
30
            $form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_RESET]);
31
        }
32
        
33
        // Load the form
34
        if (self::load($form, Yii::$app->request->post())) {
35
            $form->reset_token = Yii::$app->request->get('reset_token', null);
36
37
            // If the user is authenticated, populate the model
38
            if (!Yii::$app->user->isGuest) {
39
                $user = Yii::$app->yrc->userClass::findOne(['id' => Yii::$app->user->id]);
40
                $form->setUser($user);
41
            } else {
42
                $form->email = Yii::$app->request->post('email', null);
43
            }
44
45
            // Validate the form and make sure all of the attributes are set, then perform the reset task depending upon the scenario
46
            if ($form->validate()) {
47
                return $form->reset();
48
            } else {
49
                throw new HttpException(400, \json_encode($form->getErrors()));
50
            }
51
52
            if ($form->getScenario() === ResetPassword::SCENARIO_INIT) {
0 ignored issues
show
Unused Code introduced by
if ($form->getScenario()...T) { return true; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
53
                return true;
54
            }
55
        }
56
            
57
        return false;
58
    }
59
60
    private static function load(&$form, $attributes)
61
    {
62
        foreach ($attributes as $k => $v) {
63
            if (property_exists($form, $k)) {
64
                $form->$k = $v;
65
            }
66
        }
67
68
        return $form;
69
    }
70
}