Passed
Pull Request — master (#4)
by
unknown
05:06
created

PermissionResourceTrait::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Insenseanalytics\LaravelNovaPermission;
4
5
use Laravel\Nova\Http\Requests\NovaRequest;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Http\Requests\NovaRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Laravel\Nova\Nova;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Nova was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Laravel\Nova\Fields\ID;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Fields\ID was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Http\Request;
9
use Laravel\Nova\Fields\Text;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Fields\Text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Validation\Rule;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\Rule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Laravel\Nova\Fields\Select;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Fields\Select was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Laravel\Nova\Fields\DateTime;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Fields\DateTime was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Laravel\Nova\Fields\MorphToMany;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Fields\MorphToMany was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Laravel\Nova\Fields\BelongsToMany;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Fields\BelongsToMany was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Spatie\Permission\PermissionRegistrar;
16
17
trait PermissionResourceTrait
18
{
19
	public static function getModel()
20
	{
21
		return app(PermissionRegistrar::class)->getPermissionClass();
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

21
		return /** @scrutinizer ignore-call */ app(PermissionRegistrar::class)->getPermissionClass();
Loading history...
22
	}
23
24
	/**
25
	 * Get the fields displayed by the resource.
26
	 *
27
	 * @param \Illuminate\Http\Request $request
28
	 *
29
	 * @return array
30
	 */
31
	public function fields(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

31
	public function fields(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
	{
33
		$guardOptions = collect(config('auth.guards'))->mapWithKeys(function ($value, $key) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

33
		$guardOptions = collect(/** @scrutinizer ignore-call */ config('auth.guards'))->mapWithKeys(function ($value, $key) {
Loading history...
34
			return [$key => $key];
35
		});
36
37
		$userResource = Nova::resourceForModel(getModelForGuard($this->guard_name));
38
39
		$roleResource = Nova::resourceForModel(app(PermissionRegistrar::class)->getRoleClass());
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

39
		$roleResource = Nova::resourceForModel(/** @scrutinizer ignore-call */ app(PermissionRegistrar::class)->getRoleClass());
Loading history...
40
41
		return [
42
			ID::make()->sortable(),
43
44
			Text::make(__('laravel-nova-permission::permissions.name'), 'name')
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

44
			Text::make(/** @scrutinizer ignore-call */ __('laravel-nova-permission::permissions.name'), 'name')
Loading history...
45
				->rules(['required', 'string', 'max:255'])
46
				->creationRules('unique:' . config('permission.table_names.permissions'))
47
				->updateRules('unique:' . config('permission.table_names.permissions') . ',name,{{resourceId}}'),
48
49
            Text::make(__('laravel-nova-permission::permissions.display_name'),function (){
50
                return __('laravel-nova-permission::permissions.display_names.'.$this->name);
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

50
                return /** @scrutinizer ignore-call */ __('laravel-nova-permission::permissions.display_names.'.$this->name);
Loading history...
51
            })->canSee(function (){
52
                return is_array(__('laravel-nova-permission::permissions.display_names'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
                return is_array(/** @scrutinizer ignore-call */ __('laravel-nova-permission::permissions.display_names'));
Loading history...
53
            }),
54
55
			Select::make(__('laravel-nova-permission::permissions.guard_name'), 'guard_name')
56
				->options($guardOptions->toArray())
57
				->rules(['required', Rule::in($guardOptions)]),
58
59
			DateTime::make(__('laravel-nova-permission::permissions.created_at'), 'created_at')->exceptOnForms(),
60
61
			DateTime::make(__('laravel-nova-permission::permissions.updated_at'), 'updated_at')->exceptOnForms(),
62
63
			BelongsToMany::make($roleResource::label(), 'roles', $roleResource)->searchable(),
64
65
			MorphToMany::make($userResource::label(), 'users', $userResource)->searchable(),
66
		];
67
	}
68
69
70
    /**
71
     * Override the applyFilters method,title field translation
72
     */
73
    public function title() {
74
75
        return __('laravel-nova-permission::permissions.display_names.'.$this->name);
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

75
        return /** @scrutinizer ignore-call */ __('laravel-nova-permission::permissions.display_names.'.$this->name);
Loading history...
76
    }
77
78
    /**
79
     * Override the applyFilters method to add the guard_name condition when filtering
80
     *
81
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
82
     * @param  \Illuminate\Database\Eloquent\Builder  $query
83
     * @param  array  $filters
84
     * @return \Illuminate\Database\Eloquent\Builder
85
     */
86
    protected static function applyFilters(NovaRequest $request, $query, array $filters)
87
    {
88
        $query = parent::applyFilters($request, $query, $filters);
89
        if($model = head($request->__memoized)){
90
            $guard_name = $model->guard_name ?? getGuardForModel(get_class($model));
0 ignored issues
show
Bug introduced by
The function getGuardForModel was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

90
            $guard_name = $model->guard_name ?? /** @scrutinizer ignore-call */ getGuardForModel(get_class($model));
Loading history...
91
            $query->where('guard_name', $guard_name);
92
        }
93
94
        return $query;
95
    }
96
97
    /**
98
     * Rewrite the applySearch method to apply translation field search
99
     *
100
     * @param  \Illuminate\Database\Eloquent\Builder  $query
101
     * @param  string  $search
102
     * @return \Illuminate\Database\Eloquent\Builder
103
     */
104
    protected static function applySearch($query, $search)
105
    {
106
        $trans_search = array_keys(preg_grep("/$search/",array_dot(__('laravel-nova-permission::permissions.display_names'))));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

106
        $trans_search = array_keys(preg_grep("/$search/",array_dot(/** @scrutinizer ignore-call */ __('laravel-nova-permission::permissions.display_names'))));
Loading history...
107
108
        if (is_numeric($search) && in_array($query->getModel()->getKeyType(), ['int', 'integer'])) {
109
            $query->whereKey($search);
110
        }
111
112
        return $query->where(function ($query) use ($trans_search) {
113
            $model = $query->getModel();
114
115
            foreach (static::searchableColumns() as $column) {
116
                foreach ($trans_search as $search){
117
                    $query->orWhere($model->qualifyColumn($column), 'like', '%'.$search.'%');
118
                }
119
            }
120
        });
121
    }
122
}
123