Completed
Push — develop ( 8b1080...56861b )
by Abdelrahman
05:19
created

AbilityFormProcessRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareForValidation() 0 17 5
A rules() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Cortex\Auth\Models\Ability;
8
9
class AbilityFormProcessRequest extends AbilityFormRequest
10
{
11
    /**
12
     * Prepare the data for validation.
13
     *
14
     * @return void
15
     */
16
    protected function prepareForValidation(): void
17
    {
18
        $data = $this->all();
19
20
        // Set roles
21
        if (! empty($data['roles'])) {
22
            if ($data['roles'] && $this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
23
                $roles = array_map('intval', $this->get('roles', []));
24
                $data['roles'] = $this->user($this->route('guard'))->can('superadmin') ? $roles
25
                    : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray();
26
            } else {
27
                unset($data['roles']);
28
            }
29
        }
30
31
        $this->replace($data);
32
    }
33
34
    /**
35
     * Get the validation rules that apply to the request.
36
     *
37
     * @return array
38
     */
39
    public function rules(): array
40
    {
41
        $user = $this->route('ability') ?? new Ability();
42
        $user->updateRulesUniques();
43
        $rules = $user->getRules();
44
        $rules['roles'] = 'nullable|array';
45
46
        return $rules;
47
    }
48
}
49