Passed
Push — ft/package ( 5ee474...180175 )
by Philippe
05:16 queued 12s
created

GenerateRoleCommand::assignPermissionsToRole()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0106

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 10
nop 1
dl 0
loc 29
ccs 14
cts 15
cp 0.9333
crap 6.0106
rs 8.439
c 0
b 0
f 0
1
<?php
2
namespace Thinktomorrow\Chief\Authorization\Console;
3
4
use Thinktomorrow\Chief\Authorization\Role;
5
use Thinktomorrow\Chief\Authorization\Permission;
6
use Illuminate\Console\Command;
7
8
class GenerateRoleCommand extends Command
9
{
10
    protected $signature = 'chief:role 
11
                                {name : the role name e.g. admin, editor.}
12
                                {--permissions= : the permissions to give to this role. Assign multiple permissions by comma separated values. }';
13
14
    protected $description = 'Generate a new role';
15
16 75
    public function handle()
17
    {
18 75
        $roleName = $this->getNameArgument();
19
20 75
        $role = Role::findOrCreate($roleName, 'chief');
21
22 75
        $this->assignPermissionsToRole($role);
23 74
    }
24
25 75
    private function getNameArgument()
26
    {
27 75
        return strtolower(str_singular($this->argument('name')));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type array; however, parameter $value of str_singular() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        return strtolower(str_singular(/** @scrutinizer ignore-type */ $this->argument('name')));
Loading history...
28
    }
29
30 75
    private function assignPermissionsToRole(Role $role)
31
    {
32 75
        if (!$this->option('permissions')) {
33 1
            return;
34
        }
35
36 74
        $permissionNames = explode(',', $this->option('permissions'));
37
38 74
        $cleanPermissionNames = [];
39 74
        foreach ($permissionNames as $k => $permissionName) {
40 74
            $permissionName = trim($permissionName);
41
42
            // Generate all permissions if only scope is passed
43 74
            if (false === strpos($permissionName, '-')) {
44 72
                $cleanPermissionNames = array_merge($cleanPermissionNames, Permission::generate($permissionName));
45
            } else {
46
                // Trim the value
47 74
                $cleanPermissionNames[] = $permissionName;
48
            }
49
        }
50
51 74
        foreach ($cleanPermissionNames as $cleanPermissionName) {
52 74
            if ($role->hasPermissionTo($cleanPermissionName)) {
53
                continue;
54
            }
55 74
            $role->givePermissionTo($cleanPermissionName);
56
        }
57
58 73
        $this->info('Role ' . $role->name . ' was assigned the permissions: ' . implode(',', $cleanPermissionNames));
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Thinktomorrow\Chief\Authorization\Role. Since you implemented __get, consider adding a @property annotation.
Loading history...
59 73
    }
60
}
61