Passed
Push — master ( 3e205a...970265 )
by Philippe
66:06
created

GenerateRoleCommand::assignPermissionsToRole()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 6
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. }';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
13
14
    protected $description = 'Generate a new role';
15
16 213
    public function handle()
17
    {
18 213
        $roleName = $this->getNameArgument();
19
20 213
        $role = Role::findOrCreate($roleName, 'chief');
21
22 213
        $this->assignPermissionsToRole($role);
23 212
    }
24
25 213
    private function getNameArgument()
26
    {
27 213
        return strtolower(str_singular($this->argument('name')));
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

27
        return strtolower(/** @scrutinizer ignore-deprecated */ str_singular($this->argument('name')));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; 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 213
    private function assignPermissionsToRole(Role $role)
31
    {
32 213
        if (!$this->option('permissions')) {
33 1
            return;
34
        }
35
36 212
        $permissionNames = explode(',', $this->option('permissions'));
37
38 212
        $cleanPermissionNames = [];
39 212
        foreach ($permissionNames as $k => $permissionName) {
40 212
            $permissionName = trim($permissionName);
41
            // Generate all permissions if only scope is passed
42 212
            if (false === strpos($permissionName, '-')) {
43 210
                $cleanPermissionNames = array_merge($cleanPermissionNames, Permission::generate($permissionName));
44
            } else {
45
                // Trim the value
46 212
                $cleanPermissionNames[] = $permissionName;
47
            }
48
        }
49
50 212
        foreach ($cleanPermissionNames as $cleanPermissionName) {
51 212
            if ($role->hasPermissionTo($cleanPermissionName)) {
52 207
                continue;
53
            }
54 212
            $role->givePermissionTo($cleanPermissionName);
55
        }
56
57 211
        $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...
58 211
    }
59
}
60