Completed
Pull Request — master (#823)
by Burak
02:27
created

AssignRole   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 40
loc 40
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A handle() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\Permission\Commands;
4
5
use App\User;
6
use Illuminate\Console\Command;
7
use Spatie\Permission\Contracts\Role as RoleContract;
8
9 View Code Duplication
class AssignRole extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    protected $signature = 'permission:assign
12
        {user : User ID}
13
        {name : The name of the role}
14
        {guard? : The name of the guard}';
15
16
    protected $description = 'Assign a role';
17
18
    /**
19
     * User model.
20
     *
21
     * @var User $user
22
     */
23
    protected $user;
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @param  User  $user
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct(User $user)
32
    {
33
        parent::__construct();
34
35
        $this->user = $user;
36
    }
37
38
    public function handle()
39
    {
40
        $roleClass = app(RoleContract::class);
41
42
        $user = $this->user->find($this->argument('user'));
43
        $role = $roleClass::findByName($this->argument('name'), $this->argument('guard'));
44
45
        $user->assignRole($role->name);
46
        $this->info("Role `{$role->name}` assigned to `{$user->name}` (ID:{$user->name}) created");
47
    }
48
}
49