Permission   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A subtitle() 0 4 1
A label() 0 4 1
A singularLabel() 0 4 1
A fields() 0 37 2
1
<?php
2
3
namespace BBSLab\NovaPermission\Resources;
4
5
use BBSLab\NovaPermission\Contracts\HasAbilities;
6
use BBSLab\NovaPermission\Traits\Authorizable;
7
use BBSLab\NovaPermission\Traits\HasFieldName;
8
use Illuminate\Http\Request;
9
use Illuminate\Validation\Rule;
10
use Laravel\Nova\Fields\BelongsToMany;
11
use Laravel\Nova\Fields\DateTime;
12
use Laravel\Nova\Fields\ID;
13
use Laravel\Nova\Fields\MorphTo;
14
use Laravel\Nova\Fields\Select;
15
use Laravel\Nova\Fields\Text;
16
use Laravel\Nova\Resource;
17
18
class Permission extends Resource implements HasAbilities
19
{
20
    use Authorizable,
21
        HasFieldName;
22
23
    public static $permissionsForAbilities = [
24
        'viewAny' => 'viewAny permission',
25
        'view' => 'view permission',
26
        'create' => 'create permission',
27
        'update' => 'update permission',
28
        'delete' => 'delete permission',
29
    ];
30
31
    /**
32
     * The model the resource corresponds to.
33
     *
34
     * @var string
35
     */
36
    public static $model;
37
38
    /**
39
     * The single value that should be used to represent the resource when being displayed.
40
     *
41
     * @var string
42
     */
43
    public static $title = 'name';
44
45
    /**
46
     * Get the search result subtitle for the resource.
47
     *
48
     * @return string|null
49
     */
50
    public function subtitle()
51
    {
52
        return "Guard: {$this->guard_name}";
53
    }
54
55
    /**
56
     * The columns that should be searched.
57
     *
58
     * @var array
59
     */
60
    public static $search = [
61
        'name',
62
        'guard_name',
63
    ];
64
65
    /**
66
     * Get the displayable label of the resource.
67
     *
68
     * @return string
69
     */
70
    public static function label()
71
    {
72
        return trans('nova-permission::resources.permission.label');
73
    }
74
75
    /**
76
     * Get the displayable singular label of the resource.
77
     *
78
     * @return string
79
     */
80
    public static function singularLabel()
81
    {
82
        return trans('nova-permission::resources.permission.singular_label');
83
    }
84
85
    /**
86
     * Get the fields displayed by the resource.
87
     *
88
     * @param  \Illuminate\Http\Request  $request
89
     * @return array
90
     */
91
    public function fields(Request $request)
92
    {
93
        $guardOptions = collect(config('auth.guards'))->mapWithKeys(function ($value, $key) {
94
            return [$key => $key];
95
        });
96
97
        $fields = [
98
            ID::make()->sortable(),
99
            Text::make($this->getTranslatedFieldName('Name'), 'name')
100
                ->rules('required', 'string', 'max:255'),
101
            Text::make($this->getTranslatedFieldName('Group'), 'group')
102
                ->rules('nullable', 'string', 'max:255')
103
                ->help(trans('nova-permission::resources.permission.group_help')),
104
            Select::make($this->getTranslatedFieldName('Guard name'), 'guard_name')
105
                ->options($guardOptions->toArray())
106
                ->rules(['required', Rule::in($guardOptions)]),
107
        ];
108
109
        $models = config('nova-permission.authorizable_models', []);
110
111
        if (! empty($models)) {
112
            $fields[] = MorphTo::make($this->getTranslatedFieldName('Authorizable model'), 'authorizable')
113
                ->types($models)
114
                ->searchable()
115
                ->nullable();
116
        }
117
118
        return array_merge($fields, [
119
            DateTime::make($this->getTranslatedFieldName('Created at'), 'created_at')
120
                ->onlyOnDetail(),
121
            DateTime::make($this->getTranslatedFieldName('Updated at'), 'updated_at')
122
                ->onlyOnDetail(),
123
            BelongsToMany::make(Role::label(), 'roles', Role::class)
124
                ->searchable()
125
                ->singularLabel(Role::singularLabel()),
126
        ]);
127
    }
128
}
129