Completed
Push — master ( 8ea8de...59eb2e )
by Charles
01:52
created

OTPAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 77.78 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 49
loc 63
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B post() 27 27 5
B delete() 22 22 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace yrc\actions;
4
5
use app\models\User;
6
use yrc\rest\Action as RestAction;
7
8
use yii\web\HttpException;
9
use Yii;
10
11
/**
12
 * @class OTPAction
13
 * Handles enabling and disabling of OTP
14
 */
15
class OTPAction extends RestAction
16
{
17
    /**
18
     * [POST] /api/[...]/otp
19
     * Enables OTP for an account
20
     * @return mixed
21
     */
22 View Code Duplication
    public 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        // Find the user
25
        $user = User::findOne(Yii::$app->user->id);
26
        if ($user === null) {
27
            return false;
28
        }
29
30
        if ($user->isOTPEnabled() === true) {
31
            throw new HttpException(400, Yii::t('yrc', 'OTP is already enabled'));
32
        }
33
34
        // If an OTP code was provided, assume the account has been provisioned and just needs activation
35
        $otpVerificationCode = Yii::$app->request->post('code', false);
36
        if ($otpVerificationCode !== false) {
37
            if ($user->verifyOTP((string)$otpVerificationCode) !== false) {
38
                return $user->enableOTP();
39
            }
40
        } else {
41
            // Otherwise return the provisioning string
42
            return [
43
                'provisioning_code' => $user->provisionOTP()
44
            ];
45
        }
46
47
        return false;
48
    }
49
50
    /**
51
     * [DELETE] /api/[...]/otp
52
     * Disables OTP for an account
53
     * @return mixed
54
     */
55 View Code Duplication
    public function delete($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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        // Find the user
58
        $user = User::findOne(Yii::$app->user->id);
59
        if ($user === null) {
60
            return false;
61
        }
62
63
        if ($user->isOTPEnabled() === false) {
64
            throw new HttpException(400, Yii::t('yrc', 'Two-factor is not enabled'));
65
        }
66
67
        // Grab the code from the GET parameter, and check it
68
        $otpVerificationCode = Yii::$app->request->post('code', false);
69
        if ($otpVerificationCode !== false) {
70
            if ($user->verifyOTP((string)$otpVerificationCode) !== false) {
71
                return $user->disableOTP();
72
            }
73
        }
74
75
        return false;
76
    }
77
}