ManagerFormRequest::prepareForValidation()   C
last analyzed

Complexity

Conditions 14
Paths 128

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 6.0333
c 0
b 0
f 0
cc 14
nc 128
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 ManagerFormRequest 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
        $manager = $this->route('manager') ?? app('cortex.auth.manager');
34
        $country = $data['country_code'] ?? null;
35
        $twoFactor = $manager->getTwoFactor();
36
37
        if ($manager->exists && empty($data['password'])) {
38
            unset($data['password'], $data['password_confirmation']);
39
        }
40
41
        // Cast tenants
42
        ! $data['tenants'] || $data['tenants'] = array_map('intval', $this->get('tenants', []));
43
44
        // Set abilities
45
        if (! empty($data['abilities'])) {
46
            if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
47
                $abilities = array_map('intval', $this->get('abilities', []));
48
                $data['abilities'] = $this->user($this->route('guard'))->isA('superadmin') ? $abilities
49
                    : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray();
50
            } else {
51
                unset($data['abilities']);
52
            }
53
        }
54
55
        // Set roles
56
        if (! empty($data['roles'])) {
57
            if ($data['roles'] && $this->user($this->route('guard'))->can('assign', \Cortex\Auth\Models\Role::class)) {
58
                $roles = array_map('intval', $this->get('roles', []));
59
                $data['roles'] = $this->user($this->route('guard'))->isA('superadmin') ? $roles
60
                    : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray();
61
            } else {
62
                unset($data['roles']);
63
            }
64
        }
65
66
        if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $manager->country_code)) {
67
            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...
68
            $data['two_factor'] = $twoFactor;
69
        }
70
71
        $this->replace($data);
72
    }
73
74
    /**
75
     * Get the validation rules that apply to the request.
76
     *
77
     * @return array
78
     */
79
    public function rules(): array
80
    {
81
        $manager = $this->route('manager') ?? app('cortex.auth.manager');
82
        $manager->updateRulesUniques();
83
        $rules = $manager->getRules();
84
85
        $rules['roles'] = 'nullable|array';
86
        $rules['tenants'] = 'nullable|array';
87
        $rules['abilities'] = 'nullable|array';
88
        $rules['password'] = $manager->exists
89
            ? 'confirmed|min:'.config('cortex.auth.password_min_chars')
90
            : 'required|confirmed|min:'.config('cortex.auth.password_min_chars');
91
92
        return $rules;
93
    }
94
}
95