CreateRole   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 1
A makePermissions() 0 18 3
1
<?php
2
3
namespace Spatie\Permission\Commands;
4
5
use Illuminate\Console\Command;
6
use Spatie\Permission\Contracts\Role as RoleContract;
7
use Spatie\Permission\Contracts\Permission as PermissionContract;
8
9
class CreateRole extends Command
10
{
11
    protected $signature = 'permission:create-role
12
        {name : The name of the role}
13
        {guard? : The name of the guard}
14
        {permissions? : A list of permissions to assign to the role, separated by | }';
15
16
    protected $description = 'Create a role';
17
18
    public function handle()
19
    {
20
        $roleClass = app(RoleContract::class);
21
22
        $role = $roleClass::findOrCreate($this->argument('name'), $this->argument('guard'));
23
24
        $role->givePermissionTo($this->makePermissions($this->argument('permissions')));
25
26
        $this->info("Role `{$role->name}` created");
27
    }
28
29
    /**
30
     * @param array|null|string $string
31
     */
32
    protected function makePermissions($string = null)
33
    {
34
        if (empty($string)) {
35
            return;
36
        }
37
38
        $permissionClass = app(PermissionContract::class);
39
40
        $permissions = explode('|', $string);
41
42
        $models = [];
43
44
        foreach ($permissions as $permission) {
45
            $models[] = $permissionClass::findOrCreate(trim($permission), $this->argument('guard'));
46
        }
47
48
        return collect($models);
49
    }
50
}
51