Passed
Push — master ( 970265...da43ab )
by Philippe
64:51 queued 33:01
created

GenerateRoleCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
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. }';
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 212
    public function handle()
17
    {
18 212
        $roleName = $this->getNameArgument();
19
20 212
        $role = Role::findOrCreate($roleName, 'chief');
21
22 212
        $this->assignPermissionsToRole($role);
23 211
    }
24
25 212
    private function getNameArgument()
26
    {
27 212
        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 212
    private function assignPermissionsToRole(Role $role)
31
    {
32 212
        if (!$this->option('permissions')) {
33 1
            return;
34
        }
35
36 211
        $permissionNames = explode(',', $this->option('permissions'));
37
38 211
        $cleanPermissionNames = [];
39 211
        foreach ($permissionNames as $k => $permissionName) {
40 211
            $permissionName = trim($permissionName);
41
            // Generate all permissions if only scope is passed
42 211
            if (false === strpos($permissionName, '-')) {
43 209
                $cleanPermissionNames = array_merge($cleanPermissionNames, Permission::generate($permissionName));
44
            } else {
45
                // Trim the value
46 211
                $cleanPermissionNames[] = $permissionName;
47
            }
48
        }
49
50 211
        foreach ($cleanPermissionNames as $cleanPermissionName) {
51 211
            if ($role->hasPermissionTo($cleanPermissionName)) {
52
                continue;
53
            }
54 211
            $role->givePermissionTo($cleanPermissionName);
55
        }
56
57 210
        $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 210
    }
59
}
60