Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 13db61...ee958b )
by Cristian
01:31
created

RoleCrudController::setup()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 8
nop 0
1
<?php
2
3
namespace Backpack\PermissionManager\app\Http\Controllers;
4
5
use Backpack\CRUD\app\Http\Controllers\CrudController;
6
use Backpack\PermissionManager\app\Http\Requests\RoleStoreCrudRequest as StoreRequest;
7
use Backpack\PermissionManager\app\Http\Requests\RoleUpdateCrudRequest as UpdateRequest;
8
9
// VALIDATION
10
11
class RoleCrudController extends CrudController
12
{
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
17
18 View Code Duplication
    public function setup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        $this->role_model = $role_model = config('backpack.permissionmanager.models.role');
0 ignored issues
show
Bug introduced by
The property role_model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
        $this->permission_model = $permission_model = config('backpack.permissionmanager.models.permission');
0 ignored issues
show
Bug introduced by
The property permission_model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$permission_model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
23
        $this->crud->setModel($role_model);
24
        $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.role'), trans('backpack::permissionmanager.roles'));
25
        $this->crud->setRoute(backpack_url('role'));
26
27
        // deny access according to configuration file
28
        if (config('backpack.permissionmanager.allow_role_create') == false) {
29
            $this->crud->denyAccess('create');
30
        }
31
        if (config('backpack.permissionmanager.allow_role_update') == false) {
32
            $this->crud->denyAccess('update');
33
        }
34
        if (config('backpack.permissionmanager.allow_role_delete') == false) {
35
            $this->crud->denyAccess('delete');
36
        }
37
    }
38
39
    public function setupListOperation()
40
    {
41
        $this->crud->addColumn([
42
            'name'  => 'name',
43
            'label' => trans('backpack::permissionmanager.name'),
44
            'type'  => 'text',
45
        ]);
46
        if (config('backpack.permissionmanager.multiple_guards')) {
47
            $this->crud->addColumn([
48
                'name'  => 'guard_name',
49
                'label' => trans('backpack::permissionmanager.guard_type'),
50
                'type'  => 'text',
51
            ]);
52
        }
53
        $this->crud->addColumn([
54
            // n-n relationship (with pivot table)
55
            'label'     => ucfirst(trans('backpack::permissionmanager.permission_plural')),
56
            'type'      => 'select_multiple',
57
            'name'      => 'permissions', // the method that defines the relationship in your Model
58
            'entity'    => 'permissions', // the method that defines the relationship in your Model
59
            'attribute' => 'name', // foreign key attribute that is shown to user
60
            'model'     => $this->permission_model, // foreign key model
61
            'pivot'     => true, // on create&update, do you need to add/delete pivot table entries?
62
        ]);
63
    }
64
65
    public function setupCreateOperation() 
66
    {
67
        $this->addFields();
68
        $this->crud->setValidation(StoreRequest::class);
69
70
        //otherwise, changes won't have effect
71
        \Cache::forget('spatie.permission.cache');
72
    }
73
74
    public function setupUpdateOperation() 
75
    {
76
        $this->addFields();
77
        $this->crud->setValidation(UpdateRequest::class);
78
        
79
        //otherwise, changes won't have effect
80
        \Cache::forget('spatie.permission.cache');
81
82
    }
83
84
    private function addFields() 
85
    {
86
        $this->crud->addField([
87
            'name'  => 'name',
88
            'label' => trans('backpack::permissionmanager.name'),
89
            'type'  => 'text',
90
        ]);
91
92
        if (config('backpack.permissionmanager.multiple_guards')) {
93
            $this->crud->addField([
94
                'name'    => 'guard_name',
95
                'label'   => trans('backpack::permissionmanager.guard_type'),
96
                'type'    => 'select_from_array',
97
                'options' => $this->getGuardTypes(),
98
            ]);
99
        }
100
101
        $this->crud->addField([
102
            'label'     => ucfirst(trans('backpack::permissionmanager.permission_plural')),
103
            'type'      => 'checklist',
104
            'name'      => 'permissions',
105
            'entity'    => 'permissions',
106
            'attribute' => 'name',
107
            'model'     => $this->permission_model,
108
            'pivot'     => true,
109
        ]);
110
    }
111
112
    /*
113
     * Get an array list of all available guard types
114
     * that have been defined in app/config/auth.php
115
     *
116
     * @return array
117
     **/
118 View Code Duplication
    private function getGuardTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $guards = config('auth.guards');
121
122
        $returnable = [];
123
        foreach ($guards as $key => $details) {
124
            $returnable[$key] = $key;
125
        }
126
127
        return $returnable;
128
    }
129
}
130