Passed
Pull Request — 2.x (#727)
by Bekzat
16:53
created

UserRequest::rules()   B

Complexity

Conditions 10
Paths 3

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 22.5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 24
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 42
ccs 12
cts 24
cp 0.5
crap 22.5
rs 7.6666

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 A17\Twill\Http\Requests\Admin;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\Facades\Crypt;
7
use PragmaRX\Google2FA\Google2FA;
8
9
class UserRequest extends Request
10
{
11
    /**
12
     * Determines if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16 4
    public function authorize()
17
    {
18 4
        return true;
19
    }
20
21
    /**
22
     * Gets the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26 4
    public function rules()
27
    {
28 4
        switch ($this->method()) {
29 4
            case 'POST':
30
                {
31
                    return [
32 4
                        'name' => 'required',
33 4
                        'email' => 'required|email|unique:' . config('twill.users_table', 'twill_users') . ',email',
34 4
                        'role' => 'required|not_in:SUPERADMIN',
35
                    ];
36
                }
37 1
            case 'PUT':
38
                {
39
                    return [
40 1
                        'name' => 'required',
41 1
                        'role' => 'not_in:SUPERADMIN',
42 1
                        'email' => 'required|email|unique:' . config('twill.users_table', 'twill_users') . ',email,' . $this->route('user'),
43 1
                        'verify-code' => function ($attribute, $value, $fail) {
44
                            $user = Auth::guard('twill_users')->user();
45
                            $with2faSettings = config('twill.enabled.users-2fa') && $user->id == $this->route('user');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
47
                            if ($with2faSettings) {
48
                                $userIsEnabling = $this->get('google_2fa_enabled') && !$user->google_2fa_enabled;
0 ignored issues
show
Bug introduced by
Accessing google_2fa_enabled on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
                                $userIsDisabling = !$this->get('google_2fa_enabled') && $user->google_2fa_enabled;
50
51
                                $shouldValidateOTP = $userIsEnabling || $userIsDisabling;
52
53
                                if ($shouldValidateOTP) {
54
                                    $valid = (new Google2FA)->verifyKey($user->google_2fa_secret, $value ?? '');
0 ignored issues
show
Bug introduced by
Accessing google_2fa_secret on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
56
                                    if (!$valid) {
57
                                        $fail('Your one time password is invalid.');
58
                                    }
59
                                }
60
                            }
61 1
                        },
62
                    ];
63
                }
64
            default:break;
65
        }
66
67
        return [];
68
69
    }
70
71
}
72