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

AssignRole::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 6
loc 6
rs 10
c 0
b 0
f 0
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