AdminFormRequest::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
use Cortex\Foundation\Exceptions\GenericException;
10
11
class AdminFormRequest extends FormRequest
12
{
13
    use Escaper;
14
15
    /**
16
     * Determine if the user is authorized to make this request.
17
     *
18
     * @throws \Cortex\Foundation\Exceptions\GenericException
19
     *
20
     * @return bool
21
     */
22
    public function authorize(): bool
23
    {
24
        $currentUser = $this->user($this->route('guard'));
25
26
        if (! $currentUser->isA('superadmin') && $currentUser !== $this->route('admin')) {
27
            throw new GenericException(trans('cortex/auth::messages.action_unauthorized'), route('adminarea.admins.index'));
0 ignored issues
show
Documentation introduced by
route('adminarea.admins.index') is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28
        }
29
30
        return true;
31
    }
32
33
    /**
34
     * Prepare the data for validation.
35
     *
36
     * @return void
37
     */
38
    protected function prepareForValidation(): void
39
    {
40
        $data = $this->all();
41
42
        $admin = $this->route('admin') ?? app('cortex.auth.admin');
43
        $country = $data['country_code'] ?? null;
44
        $twoFactor = $admin->getTwoFactor();
45
46
        if ($admin->exists && empty($data['password'])) {
47
            unset($data['password'], $data['password_confirmation']);
48
        }
49
50
        // Set abilities
51
        if (! empty($data['abilities'])) {
52
            if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
53
                $abilities = array_map('intval', $this->get('abilities', []));
54
                $data['abilities'] = $this->user($this->route('guard'))->isA('superadmin') ? $abilities
55
                    : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray();
56
            } else {
57
                unset($data['abilities']);
58
            }
59
        }
60
61
        // Set roles
62
        if (! empty($data['roles'])) {
63
            if ($data['roles'] && $this->user($this->route('guard'))->can('assign', \Cortex\Auth\Models\Role::class)) {
64
                $roles = array_map('intval', $this->get('roles', []));
65
                $data['roles'] = $this->user($this->route('guard'))->isA('superadmin') ? $roles
66
                    : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray();
67
            } else {
68
                unset($data['roles']);
69
            }
70
        }
71
72
        if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $admin->country_code)) {
73
            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...
74
            $data['two_factor'] = $twoFactor;
75
        }
76
77
        $this->replace($data);
78
    }
79
80
    /**
81
     * Get the validation rules that apply to the request.
82
     *
83
     * @return array
84
     */
85
    public function rules(): array
86
    {
87
        $admin = $this->route('admin') ?? app('cortex.auth.admin');
88
        $admin->updateRulesUniques();
89
        $rules = $admin->getRules();
90
91
        $rules['roles'] = 'nullable|array';
92
        $rules['abilities'] = 'nullable|array';
93
        $rules['password'] = $admin->exists
94
            ? 'confirmed|min:'.config('cortex.auth.password_min_chars')
95
            : 'required|confirmed|min:'.config('cortex.auth.password_min_chars');
96
97
        return $this->isMethod('POST') ? $rules : [];
98
    }
99
}
100