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

AbilityFormProcessRequest::prepareForValidation()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 0
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