Completed
Push — develop ( 508664...7b00fe )
by Abdelrahman
01:58
created

AbilityFormProcessRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareForValidation() 0 15 4
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 ($data['roles'] && $this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
22
            $roles = array_map('intval', $this->get('roles', []));
23
            $data['roles'] = $this->user($this->route('guard'))->can('superadmin') ? $roles
24
                : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray();
25
        } else {
26
            unset($data['roles']);
27
        }
28
29
        $this->replace($data);
30
    }
31
32
    /**
33
     * Get the validation rules that apply to the request.
34
     *
35
     * @return array
36
     */
37
    public function rules(): array
38
    {
39
        $user = $this->route('ability') ?? new Ability();
40
        $user->updateRulesUniques();
41
        $rules = $user->getRules();
42
        $rules['roles'] = 'nullable|array';
43
44
        return $rules;
45
    }
46
}
47