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

RoleFormProcessRequest   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\Role;
8
9
class RoleFormProcessRequest extends RoleFormRequest
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 abilities
21
        if ($data['abilities'] && $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...
22
            $abilities = array_map('intval', $this->get('abilities', []));
23
            $data['abilities'] = $this->user($this->route('guard'))->can('superadmin') ? $abilities
24
                : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray();
25
        } else {
26
            unset($data['abilities']);
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('role') ?? new Role();
40
        $user->updateRulesUniques();
41
        $rules = $user->getRules();
42
        $rules['abilities'] = 'nullable|array';
43
44
        return $rules;
45
    }
46
}
47