Completed
Push — develop ( 5abd4c...dae2aa )
by Abdelrahman
09:07
created

RoleFormRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
A prepareForValidation() 0 14 3
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
use Illuminate\Foundation\Http\FormRequest;
9
10
class RoleFormRequest extends FormRequest
11
{
12
    /**
13
     * Determine if the user is authorized to make this request.
14
     *
15
     * @return bool
16
     */
17
    public function authorize(): bool
18
    {
19
        return true;
20
    }
21
22
    /**
23
     * Prepare the data for validation.
24
     *
25
     * @return void
26
     */
27
    protected function prepareForValidation(): void
28
    {
29
        $data = $this->all();
30
31
        // Set abilities
32
        if ($this->user($this->get('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
33
            $data['abilities'] = $this->user($this->get('guard'))->can('superadmin') ? $this->get('abilities', [])
34
                : $this->user($this->get('guard'))->abilities->pluck('id')->intersect($this->get('abilities', []))->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 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...
35
        } else {
36
            unset($data['abilities']);
37
        }
38
39
        $this->replace($data);
40
    }
41
42
    /**
43
     * Get the validation rules that apply to the request.
44
     *
45
     * @return array
46
     */
47
    public function rules(): array
48
    {
49
        $user = $this->route('role') ?? new Role();
50
        $user->updateRulesUniques();
51
        $rules = $user->getRules();
52
        $rules['abilities'] = 'nullable|array';
53
54
        return $rules;
55
    }
56
}
57