Completed
Push — master ( 63ef87...71e28f )
by Chris
10s
created

CreateRole::makePermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
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
    protected function makePermissions($string = null)
30
    {
31
        $permissionClass = app(PermissionContract::class);
32
33
        $permissions = explode('|', $string);
34
35
        $models = [];
36
37
        foreach ($permissions as $permission) {
38
            $models[] = $permissionClass::findOrCreate(trim($permission), $this->argument('guard'));
39
        }
40
41
        return collect($models);
42
    }
43
}
44