MemberFormRequest::prepareForValidation()   C
last analyzed

Complexity

Conditions 13
Paths 64

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 6.6166
c 0
b 0
f 0
cc 13
nc 64
nop 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
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Rinvex\Support\Traits\Escaper;
8
use Illuminate\Foundation\Http\FormRequest;
9
10
class MemberFormRequest extends FormRequest
11
{
12
    use Escaper;
13
14
    /**
15
     * Determine if the user is authorized to make this request.
16
     *
17
     * @return bool
18
     */
19
    public function authorize(): bool
20
    {
21
        return true;
22
    }
23
24
    /**
25
     * Prepare the data for validation.
26
     *
27
     * @return void
28
     */
29
    protected function prepareForValidation(): void
30
    {
31
        $data = $this->all();
32
33
        $member = $this->route('member') ?? app('cortex.auth.member');
34
        $country = $data['country_code'] ?? null;
35
        $twoFactor = $member->getTwoFactor();
36
37
        if ($member->exists && empty($data['password'])) {
38
            unset($data['password'], $data['password_confirmation']);
39
        }
40
41
        // Set abilities
42
        if (! empty($data['abilities'])) {
43
            if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
44
                $abilities = array_map('intval', $this->get('abilities', []));
45
                $data['abilities'] = $this->user($this->route('guard'))->isA('superadmin') ? $abilities
46
                    : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray();
47
            } else {
48
                unset($data['abilities']);
49
            }
50
        }
51
52
        // Set roles
53
        if (! empty($data['roles'])) {
54
            if ($data['roles'] && $this->user($this->route('guard'))->can('assign', \Cortex\Auth\Models\Role::class)) {
55
                $roles = array_map('intval', $this->get('roles', []));
56
                $data['roles'] = $this->user($this->route('guard'))->isA('superadmin') ? $roles
57
                    : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray();
58
            } else {
59
                unset($data['roles']);
60
            }
61
        }
62
63
        if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $member->country_code)) {
64
            array_set($twoFactor, 'phone.enabled', false);
0 ignored issues
show
Deprecated Code introduced by
The function array_set() has been deprecated with message: Arr::set() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
65
            $data['two_factor'] = $twoFactor;
66
        }
67
68
        $this->replace($data);
69
    }
70
71
    /**
72
     * Get the validation rules that apply to the request.
73
     *
74
     * @return array
75
     */
76
    public function rules(): array
77
    {
78
        $member = $this->route('member') ?? app('cortex.auth.member');
79
        $member->updateRulesUniques();
80
        $rules = $member->getRules();
81
82
        $rules['roles'] = 'nullable|array';
83
        $rules['abilities'] = 'nullable|array';
84
        $rules['password'] = $member->exists
85
            ? 'confirmed|min:'.config('cortex.auth.password_min_chars')
86
            : 'required|confirmed|min:'.config('cortex.auth.password_min_chars');
87
88
        return $rules;
89
    }
90
}
91