RoleFormProcessRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareForValidation() 0 17 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 (! empty($data['abilities'])) {
22
            if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
23
                $abilities = array_map('intval', $this->get('abilities', []));
24
                $data['abilities'] = $this->user($this->route('guard'))->isA('superadmin') ? $abilities
25
                    : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray();
26
            } else {
27
                unset($data['abilities']);
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('role') ?? new Role();
42
        $user->updateRulesUniques();
43
        $rules = $user->getRules();
44
        $rules['abilities'] = 'nullable|array';
45
46
        return $rules;
47
    }
48
}
49