Completed
Push — develop ( 87574f...25eb07 )
by Abdelrahman
11:56
created

AccountSettingsRequest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 14
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
C prepareForValidation() 0 31 7
B rules() 0 29 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Requests\Frontarea;
6
7
use Illuminate\Foundation\Http\FormRequest;
8
9
class AccountSettingsRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize()
17
    {
18
        return true;
19
    }
20
21
    /**
22
     * Prepare the data for validation.
23
     *
24
     * @return void
25
     */
26
    protected function prepareForValidation()
27
    {
28
        $data = $this->all();
29
30
        $country = $data['country_code'] ?? null;
31
        $email = $data['email'] ?? null;
32
        $phone = $data['phone'] ?? null;
33
        $user = $this->user();
34
        $twoFactor = $user->getTwoFactor();
35
36
        if (empty($data['password'])) {
37
            unset($data['password'], $data['password_confirmation']);
38
        }
39
40
        if ($email !== $user->email) {
41
            $data['email_verified'] = false;
42
            $data['email_verified_at'] = null;
43
        }
44
45
        if ($phone !== $user->phone) {
46
            $data['phone_verified'] = false;
47
            $data['phone_verified_at'] = null;
48
        }
49
50
        if ($twoFactor && (isset($data['phone_verified']) || $country !== $user->country_code)) {
51
            array_set($twoFactor, 'phone.enabled', false);
52
            $data['two_factor'] = $twoFactor;
53
        }
54
55
        $this->replace($data);
56
    }
57
58
    /**
59
     * Get the validation rules that apply to the request.
60
     *
61
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
62
     */
63
    public function rules()
64
    {
65
        $user = $this->user();
66
        $user->updateRulesUniques();
67
        $rules = $user->getRules();
68
69
        // Attach attribute rules
70
        $user->getEntityAttributes()->each(function ($attribute, $attributeSlug) use (&$rules) {
71
            switch ($attribute->type) {
72
                case 'datetime':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73
                    $type = 'date';
74
                    break;
75
                case 'text':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
76
                case 'varchar':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
77
                    $type = 'string';
78
                    break;
79
                default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
80
                    $type = $attribute->type;
81
                    break;
82
            }
83
84
            $rule = ($attribute->is_required ? 'required|' : 'nullable|').$type;
85
            $rules[$attributeSlug.($attribute->is_collection ? '.*' : '')] = $rule;
86
        });
87
88
        $rules['password'] = 'sometimes|required|confirmed|min:'.config('rinvex.fort.password_min_chars');
89
90
        return $rules;
91
    }
92
}
93