GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SeedDefaultMenus   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 136
rs 10
c 1
b 0
f 0
wmc 7
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 119 6
1
<?php namespace Afrittella\BackProject\Console\Commands;
2
3
use Afrittella\BackProject\Models\Menu;
4
use Afrittella\BackProject\Models\Role;
5
use Afrittella\BackProject\Models\Permission;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\DB;
8
9
10
class SeedDefaultMenus extends Command
11
{
12
    protected $signature = 'back-project:seed-menus';
13
14
    protected $description = 'Seed default admin-menu';
15
16
    public function __construct()
17
    {
18
        parent::__construct();
19
    }
20
21
    /**
22
     * Execute the console command.
23
     *
24
     * @return mixed
25
     */
26
    public function handle()
27
    {
28
29
        $nodes = [
30
        'name' => 'admin-menu',
31
        'title' => 'Administrator Menu',
32
        'description' => 'A default menu you can use for back office purposes',
33
        'route' => null,
34
        'icon' => null,
35
        'is_active' => 1,
36
        'is_protected' => 1,
37
        'children' => [
38
            [
39
            'name' => 'dashboard',
40
            'permission' => 'backend',
41
            'title' => 'Dashboard',
42
            'description' => 'Your dashboard',
43
            'route' => config('back-project.route_prefix').'/dashboard',
44
            'icon' => 'fa fa-dashboard',
45
            'is_active' => 1,
46
            'is_protected' => 1,
47
            ],
48
            [
49
            'name' => 'auth',
50
            'title' => 'Authorization',
51
            'description' => 'Manage Users, Roles and Permissions',
52
            'route' => null,
53
            'icon' => 'fa fa-key',
54
            'is_active' => 1,
55
            'children' => [
56
                [
57
                'name' => 'users',
58
                'permission' => 'administration',
59
                'title' => 'Users',
60
                'description' => 'Manage Users',
61
                'route' => config('back-project.route_prefix').'/users',
62
                'icon' => 'fa fa-users',
63
                'is_active' => 1
64
                ],
65
                [
66
                'name' => 'roles',
67
                'permission' => 'administration',
68
                'title' => 'Roles',
69
                'description' => 'Manage Roles',
70
                'route' => config('back-project.route_prefix').'/roles',
71
                'icon' => 'fa fa-group',
72
                'is_active' => 1
73
                ],
74
                [
75
                'name' => 'permissions',
76
                'permission' => 'administration',
77
                'title' => 'Permissions',
78
                'description' => 'Manage Permissions',
79
                'route' => config('back-project.route_prefix').'/permissions',
80
                'icon' => 'fa fa-group',
81
                'is_active' => 1
82
                ]
83
            ]
84
            ],
85
            [
86
            'name' => 'menus',
87
            'permission' => 'administration',
88
            'title' => 'Menus',
89
            'description' => 'Manage menus',
90
            'route' => config('back-project.route_prefix').'/menus',
91
            'icon' => 'fa fa-ellipsis-v',
92
            'is_active' => 1,
93
            'is_protected' => 1,
94
            ],
95
            [
96
            'name' => 'attachments',
97
            'permission' => 'backend',
98
            'title' => 'My Media',
99
            'description' => 'Manage attachments',
100
            'route' => config('back-project.route_prefix').'/attachments',
101
            'icon' => 'fa fa-user-circle-o',
102
            'is_active' => 1,
103
            'is_protected' => 1,
104
            ],
105
            [
106
            'name' => 'admin-attachments',
107
            'permission' => 'administration',
108
            'title' => 'All Media',
109
            'description' => 'Manage all user\'s attachments',
110
            'route' => config('back-project.route_prefix').'/media',
111
            'icon' => 'fa fa-file-image-o',
112
            'is_active' => 1,
113
            'is_protected' => 1,
114
            ],
115
        ]
116
        ];
117
118
        // Truncate menu table
119
        DB::table('menus')->truncate();
120
121
        Menu::create($nodes);
122
123
        $menus = DB::table('menus')->select('permission')->where('permission', '<>', '')->groupBy('permission')->get();
124
125
        $role = Role::where('name', '=', 'administrator')->first();
126
        $user_role = Role::where('name', '=', 'user')->first();
127
128
        foreach ($menus as $menu):
129
130
          if (!empty($menu->permission)) {
131
                $permission = Permission::firstOrCreate(['name' => $menu->permission]);
0 ignored issues
show
Unused Code introduced by
$permission 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...
132
133
                if (!$role->hasPermissionTo($menu->permission)) {
134
                    $role->givePermissionTo($menu->permission);
135
                }
136
137
                if ($menu->permission == 'backend' and !$role->hasPermissionTo($menu->permission)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
138
                    $user_role->givePermissionTo($menu->permission);
139
                }
140
            }
141
        endforeach;
142
143
        $this->info('Seeding admin-menu'.'...');
144
    }
145
}
146