UpdateRole   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 29
c 1
b 0
f 0
dl 0
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A messages() 0 9 1
A authorize() 0 3 2
A attributes() 0 6 1
A rules() 0 20 1
1
<?php
2
3
namespace Itstructure\LaRbac\Http\Requests;
4
5
use Illuminate\Validation\Rule;
6
use Illuminate\Foundation\Http\FormRequest;
7
8
/**
9
 * Class UpdateRole
10
 *
11
 * @package Itstructure\LaRbac\Http\Requests
12
 *
13
 * @author Andrey Girnik <[email protected]>
14
 */
15
class UpdateRole extends FormRequest
16
{
17
    /**
18
     * Determine if the user is authorized to make this request.
19
     *
20
     * @return bool
21
     */
22
    public function authorize()
23
    {
24
        return !empty(config('rbac.routesMainPermission')) ? $this->user()->can(config('rbac.routesMainPermission')) : true;
25
    }
26
27
    /**
28
     * Get the validation rules that apply to the request.
29
     *
30
     * @return array
31
     */
32
    public function rules()
33
    {
34
        $id = $this->route('role')->id;
35
        return [
36
            'name' => [
37
                'required',
38
                'string',
39
                'regex:/^[\w\s\-]+$/',
40
                'min:3',
41
                'max:191',
42
                Rule::unique('roles')->where('id', '<>', $id),
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Validation\Rules\Unique::where() has too many arguments starting with $id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
                Rule::unique('roles')->/** @scrutinizer ignore-call */ where('id', '<>', $id),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
43
            ],
44
            'description' => [
45
                'required',
46
                'string',
47
                'min:3',
48
                'max:191',
49
                Rule::unique('roles')->where('id', '<>', $id),
50
            ],
51
            'permissions' => 'required|array',
52
        ];
53
    }
54
55
    /**
56
     * Get the error messages for the defined validation rules.
57
     *
58
     * @return array
59
     */
60
    public function messages()
61
    {
62
        return [
63
            'required' => __('rbac::validation.required'),
64
            'string' => __('rbac::validation.string'),
65
            'regex' => __('rbac::validation.regex'),
66
            'min' => __('rbac::validation.min'),
67
            'max' => __('rbac::validation.max'),
68
            'unique' => __('rbac::validation.unique'),
69
        ];
70
    }
71
72
    /**
73
     * Get custom attributes for validator errors.
74
     *
75
     * @return array
76
     */
77
    public function attributes()
78
    {
79
        return [
80
            'name' => __('rbac::main.name'),
81
            'description' => __('rbac::main.description'),
82
            'permissions' => __('rbac::permissions.permissions'),
83
        ];
84
    }
85
}
86