GenerateRolesPermissions::handle()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 4
eloc 35
c 3
b 1
f 1
nc 8
nop 0
dl 0
loc 48
rs 9.36
1
<?php
2
3
namespace Chuckbe\Chuckcms\Commands;
4
5
use Illuminate\Console\Command;
6
use Spatie\Permission\Models\Permission;
7
use Spatie\Permission\Models\Role;
8
9
class GenerateRolesPermissions extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'chuckcms:generate-roles-permissions';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'This command generates all default roles and permissions.';
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $this->info('Generating roles and permissions...');
43
44
        // Reset cached roles and permissions
45
        app()['cache']->forget('spatie.permission.cache');
46
47
        $roles = array_keys(config('chuckcms.permissions'));
48
        $permissions = [];
49
50
        foreach ($roles as $role) {
51
            $permissions = [...$permissions, ...config('chuckcms.permissions.'.$role)];
52
        }
53
54
        $permissions = array_unique($permissions);
55
56
        foreach ($permissions as $permission) {
57
            Permission::firstOrCreate(['name' => $permission]);
58
        }
59
60
        foreach ($roles as $role) {
61
            $roleObj = Role::firstOrCreate(['name' => $role]);
62
            $roleObj->revokePermissionTo(Permission::all());
63
            $roleObj->givePermissionTo(config('chuckcms.permissions.'.$role));
64
        }
65
66
        $this->info('   .      .');
67
        $this->info('  ..       ..');
68
        $this->info(' ...        ...');
69
        $this->info('.... AWESOME ....');
70
        $this->info('...         ...');
71
        $this->info('..         ..');
72
        $this->info('.         .');
73
        $this->info('.         .');
74
        $this->info('..         ..');
75
        $this->info('...         ...');
76
        $this->info('....   JOB   ....');
77
        $this->info('...         ...');
78
        $this->info('..         ..');
79
        $this->info('.         .');
80
        $this->info('───────────────▄▄───▐█');
81
        $this->info('───▄▄▄───▄██▄──█▀───█─▄');
82
        $this->info('─▄██▀█▌─██▄▄──▐█▀▄─▐█▀');
83
        $this->info('▐█▀▀▌───▄▀▌─▌─█─▌──▌─▌');
84
        $this->info('▌▀▄─▐──▀▄─▐▄─▐▄▐▄─▐▄─▐▄');
85
        $this->info(' ');
86
        $this->info('Successfully generated all default roles and permissions.');
87
        $this->info(' ');
88
    }
89
}
90