ResetPasswordAction::post()   B
last analyzed

Complexity

Conditions 10
Paths 25

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 26
nc 25
nop 1
dl 0
loc 48
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace yrc\actions;
4
5
use common\forms\ResetPassword;
0 ignored issues
show
Bug introduced by
The type common\forms\ResetPassword was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use yrc\rest\Action as RestAction;
7
use yrc\models\redis\Code;
8
9
use yii\web\HttpException;
10
use Yii;
11
12
/**
13
 * Handles token refresh
14
 * @class ResetPasswordAction
15
 */
16
class ResetPasswordAction extends RestAction
17
{
18
    const SCENARIO_TOKENIZED = 'tokenized';
19
    const SCENARIO_AUTHENTICATED = 'authenticated';
20
21
    /**
22
     * The ResetPassword scenario to use
23
     * @var string $scenario
24
     */
25
    public $scenario;
26
27
    /**
28
     * Reset password flow
29
     * @param array $params
30
     * @return boolean
31
     */
32
    public function post($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
    public function post(/** @scrutinizer ignore-unused */ $params)

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

Loading history...
33
    {
34
        static $form;
35
        if ($this->scenario === null || $this->scenario === static::SCENARIO_TOKENIZED) {
36
            $token = Yii::$app->request->get('reset_token', false);
37
38
            // Determine the correct scenario to use based upon the reset token
39
            if ($token === false) {
40
                $form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_INIT]);
41
            } else {
42
                $form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_RESET]);
43
            }
44
45
            // If the user is authenticated, populate the model
46
            if (!Yii::$app->user->isGuest) {
47
                $user = Yii::$app->user->identityClass::findOne(['id' => Yii::$app->user->id]);
48
                $form->setUser($user);
49
            } else {
50
                $form->email = Yii::$app->request->post('email', null);
51
            }
52
53
            $form->reset_token = Yii::$app->request->get('reset_token', null);
54
        } elseif ($this->scenario === static::SCENARIO_AUTHENTICATED) {
55
            if (Yii::$app->user->isGuest) {
56
                throw new HttpException(400, Yii::t('yrc', 'You must be authenticated to reset your password'));
57
                return;
0 ignored issues
show
Unused Code introduced by
return is not 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...
58
            }
59
60
            $form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_RESET_AUTHENTICATED]);
61
            $form->user_id = Yii::$app->user->id;
62
        }
63
64
        // Load the model using the helper method
65
        if (self::load($form, Yii::$app->request->post())) {
66
            // If the form is valid, reset the password
67
            if ($form->validate()) {
68
                return $form->reset();
69
            }
70
71
            // If a password reset was requested, (init) return true ALWAYS
72
            if ($form->getScenario() === ResetPassword::SCENARIO_INIT) {
73
                return true;
74
            }
75
76
            throw new HttpException(400, \json_encode($form->getErrors()));
77
        }
78
            
79
        return false;
80
    }
81
82
    private static function load(&$form, $attributes)
83
    {
84
        foreach ($attributes as $k => $v) {
85
            if (property_exists($form, $k)) {
86
                $form->$k = $v;
87
            }
88
        }
89
90
        return $form;
91
    }
92
}
93